<p>一、需求:把字符串按照给定拆分符(字符或字符串)拆分开:</p> <p>例如:</p> <p>a.字符拆分:</p> <p>a1: 1001<span style="background-color: #ffffff; color: #ff0000">,</span>ziweiyi<span style="color: #ff0000">,</span>abcd  (末尾无拆分符) 按照' ,'拆分开成:1001 ziweiyi abcd</p> <p>a2: 1001<span style="color: #ff0000">,</span>ziweiyi<span style="color: #ff0000">,</span>abcd<span style="color: #ff0000">,</span> (末尾有拆分符) 按照' ,'拆分开成:1001 ziweiyi abcd</p> <p> </p> <p>b.字符串拆分:</p> <p>b1: 1001<span style="color: #ff0000">br</span>ziweiyi<span style="color: #ff0000">br</span>abcd    (末尾无拆分符) 按照“<span>br</span>”拆分开成:1001 ziweiyi abcd</p> <p>b2: 1001<span style="color: #ff0000">br</span>ziweiyi<span style="color: #ff0000">br</span>abcd<span style="color: #ff0000">br</span> (末尾有拆分符) 按照“ <span>br</span>”拆分开成:1001 ziweiyi abcd</p> <p> </p> <p>二、上代码:这里提供两个可用拆分函数,请根据字喜好选择使用</p> <p>方法一:</p> <div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">void</span> SplitString(<span style="color: #0000ff">char</span>* src,<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span>* separator, vector<<span style="color: #0000ff">string</span>>&<span style="color: #000000"> destVector) </span><span style="color: #008080"> 2</span> <span style="color: #000000">{ </span><span style="color: #008080"> 3</span> <span style="color: #008080"> 4</span> <span style="color: #000000"> destVector.clear(); </span><span style="color: #008080"> 5</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">0</span>==<span style="color: #000000">strlen(separator)) </span><span style="color: #008080"> 6</span> <span style="color: #000000"> { </span><span style="color: #008080"> 7</span> destVector.push_back(<span style="color: #0000ff">string</span><span style="color: #000000">(src)); </span><span style="color: #008080"> 8</span> <span style="color: #0000ff">return</span><span style="color: #000000"> ; </span><span style="color: #008080"> 9</span> <span style="color: #000000"> } </span><span style="color: #008080">10</span> <span style="color: #008080">11</span> <span style="color: #0000ff">char</span> *first,*<span style="color: #000000">second; </span><span style="color: #008080">12</span> <span style="color: #0000ff">char</span> tmp[<span style="color: #800080">50</span><span style="color: #000000">]; </span><span style="color: #008080">13</span> first =<span style="color: #000000"> src; </span><span style="color: #008080">14</span> <span style="color: #0000ff">while</span><span style="color: #000000">(first) </span><span style="color: #008080">15</span> <span style="color: #000000"> { </span><span style="color: #008080">16</span> memset(tmp,<span style="color: #800000">'</span><span style="color: #800000">\0</span><span style="color: #800000">'</span>,<span style="color: #0000ff">sizeof</span><span style="color: #000000">(tmp)); </span><span style="color: #008080">17</span> second =<span style="color: #000000"> strstr(first,separator); </span><span style="color: #008080">18</span> <span style="color: #0000ff">if</span> (NULL==<span style="color: #000000">second) </span><span style="color: #008080">19</span> <span style="color: #000000"> { </span><span style="color: #008080">20</span> <span style="color: #000000"> strncpy(tmp,first,strlen(src)); </span><span style="color: #008080">21</span> <span style="color: #000000"> } </span><span style="color: #008080">22</span> <span style="color: #0000ff">else</span> <span style="color: #008080">23</span> <span style="color: #000000"> { </span><span style="color: #008080">24</span> strncpy(tmp,first,second-<span style="color: #000000">first); </span><span style="color: #008080">25</span> <span style="color: #000000"> } </span><span style="color: #008080">26</span> <span style="color: #008080">27</span> <span style="color: #0000ff">if</span> (strlen(tmp)!=<span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">28</span> <span style="color: #000000"> { </span><span style="color: #008080">29</span> destVector.push_back(<span style="color: #0000ff">string</span><span style="color: #000000">(tmp)); </span><span style="color: #008080">30</span> <span style="color: #000000"> } </span><span style="color: #008080">31</span> first = second +<span style="color: #000000">strlen(separator); </span><span style="color: #008080">32</span> <span style="color: #008080">33</span> <span style="color: #0000ff">if</span> (NULL==<span style="color: #000000">second) </span><span style="color: #008080">34</span> <span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #008080">35</span> <span style="color: #000000"> } </span><span style="color: #008080">36</span> <span style="color: #008080">37</span> }</pre> </div>
<p>方法二:</p>
<div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">int</span> SplitString(<span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> src, <span style="color: #0000ff">string</span> separator, vector<<span style="color: #0000ff">string</span>>&<span style="color: #000000"> destVector) </span><span style="color: #008080"> 2</span> <span style="color: #000000">{ </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">string</span><span style="color: #000000"> temp, SrcTemp; </span><span style="color: #008080"> 4</span> <span style="color: #0000ff">int</span> nPos = <span style="color: #800080">0</span>, nResultCount = <span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #008080"> 5</span> <span style="color: #0000ff">bool</span> bAddEmpty = <span style="color: #0000ff">false</span><span style="color: #000000">; </span><span style="color: #008080"> 6</span> <span style="color: #000000"> destVector.clear(); </span><span style="color: #008080"> 7</span> SrcTemp =<span style="color: #000000"> src; </span><span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> <span style="color: #0000ff">if</span><span style="color: #000000">(separator.empty()) </span><span style="color: #008080">10</span> <span style="color: #000000"> { </span><span style="color: #008080">11</span> <span style="color: #000000"> destVector.push_back(SrcTemp); </span><span style="color: #008080">12</span> <span style="color: #0000ff">return</span> <span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #008080">13</span> <span style="color: #000000"> } </span><span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #0000ff">do</span> <span style="color: #008080">16</span> <span style="color: #000000"> { </span><span style="color: #008080">17</span> nPos =<span style="color: #000000"> SrcTemp.find(separator); </span><span style="color: #008080">18</span> <span style="color: #008080">19</span> <span style="color: #0000ff">if</span>(nPos != <span style="color: #0000ff">string</span><span style="color: #000000">::npos) </span><span style="color: #008080">20</span> <span style="color: #000000"> { </span><span style="color: #008080">21</span> <span style="color: #0000ff">if</span>(nPos == <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">22</span> <span style="color: #000000"> { </span><span style="color: #008080">23</span> SrcTemp = SrcTemp.substr(nPos +<span style="color: #000000"> separator.length(), </span><span style="color: #008080">24</span> SrcTemp.length() - nPos -<span style="color: #000000"> separator.length()); </span><span style="color: #008080">25</span> <span style="color: #0000ff">continue</span><span style="color: #000000">; </span><span style="color: #008080">26</span> <span style="color: #000000"> } </span><span style="color: #008080">27</span> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(nPos > <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">28</span> <span style="color: #000000"> { </span><span style="color: #008080">29</span> temp = SrcTemp.substr(<span style="color: #800080">0</span><span style="color: #000000">, nPos); </span><span style="color: #008080">30</span> <span style="color: #008080">31</span> SrcTemp = SrcTemp.substr(nPos +<span style="color: #000000"> separator.length(), </span><span style="color: #008080">32</span> SrcTemp.length() - nPos -<span style="color: #000000"> separator.length()); </span><span style="color: #008080">33</span> <span style="color: #0000ff">if</span> (temp.length() > <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">34</span> <span style="color: #000000"> { </span><span style="color: #008080">35</span> <span style="color: #000000"> destVector.push_back(temp); </span><span style="color: #008080">36</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">37</span> <span style="color: #000000"> } </span><span style="color: #008080">38</span> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span><span style="color: #000000"> (bAddEmpty) </span><span style="color: #008080">39</span> <span style="color: #000000"> { </span><span style="color: #008080">40</span> <span style="color: #000000"> destVector.push_back(temp); </span><span style="color: #008080">41</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">42</span> <span style="color: #000000"> } </span><span style="color: #008080">43</span> <span style="color: #008080">44</span> <span style="color: #000000"> } </span><span style="color: #008080">45</span> <span style="color: #000000"> } </span><span style="color: #008080">46</span> <span style="color: #0000ff">else</span> <span style="color: #008080">47</span> <span style="color: #000000"> { </span><span style="color: #008080">48</span> <span style="color: #0000ff">if</span>(nResultCount == <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">49</span> <span style="color: #000000"> { </span><span style="color: #008080">50</span> <span style="color: #0000ff">if</span> (SrcTemp.length() > <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">51</span> <span style="color: #000000"> { </span><span style="color: #008080">52</span> <span style="color: #000000"> destVector.push_back(SrcTemp); </span><span style="color: #008080">53</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">54</span> <span style="color: #000000"> } </span><span style="color: #008080">55</span> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span><span style="color: #000000"> (bAddEmpty) </span><span style="color: #008080">56</span> <span style="color: #000000"> { </span><span style="color: #008080">57</span> <span style="color: #000000"> destVector.push_back(SrcTemp); </span><span style="color: #008080">58</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">59</span> <span style="color: #000000"> } </span><span style="color: #008080">60</span> SrcTemp = <span style="color: #800000">""</span><span style="color: #000000">; </span><span style="color: #008080">61</span> <span style="color: #000000"> } </span><span style="color: #008080">62</span> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(nResultCount > <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">63</span> <span style="color: #000000"> { </span><span style="color: #008080">64</span> <span style="color: #0000ff">if</span> (SrcTemp.length() > <span style="color: #800080">0</span><span style="color: #000000">) </span><span style="color: #008080">65</span> <span style="color: #000000"> { </span><span style="color: #008080">66</span> <span style="color: #000000"> destVector.push_back(SrcTemp); </span><span style="color: #008080">67</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">68</span> <span style="color: #000000"> } </span><span style="color: #008080">69</span> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span><span style="color: #000000"> (bAddEmpty) </span><span style="color: #008080">70</span> <span style="color: #000000"> { </span><span style="color: #008080">71</span> <span style="color: #000000"> destVector.push_back(SrcTemp); </span><span style="color: #008080">72</span> nResultCount++<span style="color: #000000">; </span><span style="color: #008080">73</span> <span style="color: #000000"> } </span><span style="color: #008080">74</span> SrcTemp = <span style="color: #800000">""</span><span style="color: #000000">; </span><span style="color: #008080">75</span> <span style="color: #000000"> } </span><span style="color: #008080">76</span> <span style="color: #0000ff">else</span> <span style="color: #008080">77</span> <span style="color: #0000ff">return</span> -<span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #008080">78</span> <span style="color: #000000"> } </span><span style="color: #008080">79</span> } <span style="color: #0000ff">while</span>(!<span style="color: #000000">SrcTemp.empty()); </span><span style="color: #008080">80</span> <span style="color: #008080">81</span> <span style="color: #0000ff">return</span><span style="color: #000000"> nResultCount; </span><span style="color: #008080">82</span> }</pre> </div>
<p>三、测试main</p>
<div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main() </span><span style="color: #008080"> 2</span> <span style="color: #000000">{ </span><span style="color: #008080"> 3</span> vector<<span style="color: #0000ff">string</span>><span style="color: #000000"> vecTar; </span><span style="color: #008080"> 4</span> <span style="color: #0000ff">char</span> szSrc[]=<span style="color: #800000">"</span><span style="color: #800000">1001,ziweiyi,abcd</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008080"> 5</span> SplitString(szSrc, <span style="color: #800000">"</span><span style="color: #800000">,</span><span style="color: #800000">"</span>, vecTar);<span style="color: #008000">//</span><span style="color: #008000">方法一</span> <span style="color: #008080"> 6</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = <span style="color: #800080">0</span>; i < vecTar.size();i ++<span style="color: #000000">) </span><span style="color: #008080"> 7</span> <span style="color: #000000"> { </span><span style="color: #008080"> 8</span> printf(<span style="color: #800000">"</span><span style="color: #800000">【%s】 SplitString: %s\n</span><span style="color: #800000">"</span><span style="color: #000000">,szSrc,vecTar[i].c_str()); </span><span style="color: #008080"> 9</span> <span style="color: #000000"> } </span><span style="color: #008080">10</span> printf(<span style="color: #800000">"</span><span style="color: #800000">方法1,end.\n\n</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">11</span> <span style="color: #008080">12</span> <span style="color: #0000ff">string</span> strSrc=<span style="color: #800000">"</span><span style="color: #800000">1001,ziweiyi,abcd</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008080">13</span> <span style="color: #0000ff">int</span> nRet = SplitString(strSrc, <span style="color: #800000">"</span><span style="color: #800000">,</span><span style="color: #800000">"</span>, vecTar);<span style="color: #008000">//</span><span style="color: #008000">方法二</span> <span style="color: #008080">14</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = <span style="color: #800080">0</span>; i < nRet;i ++<span style="color: #000000">) </span><span style="color: #008080">15</span> <span style="color: #000000"> { </span><span style="color: #008080">16</span> printf(<span style="color: #800000">"</span><span style="color: #800000">【%s】 SplitString: %s\n</span><span style="color: #800000">"</span><span style="color: #000000">,strSrc.c_str(),vecTar[i].c_str()); </span><span style="color: #008080">17</span> <span style="color: #000000"> } </span><span style="color: #008080">18</span> printf(<span style="color: #800000">"</span><span style="color: #800000">方法2,end.\n\n</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">19</span> <span style="color: #008080">20</span> }</pre> </div>
<p>这里指给出了最常见的案例(需求a1的情况),其他的可以自己测试,本人已测试过。</p>
<p>贴出测试效果,更明白:</p>
<p>   a1: 1001<span style="color: #ff0000">,</span>ziweiyi<span style="color: #ff0000">,</span>abcd  (末尾无拆分符) 按照'<span style="color: #ff0000"> ,</span>'拆分开成:1001 ziweiyi abcd</p>
<p>   <img alt="" src="http://images.cnitblog.com/blog/313639/201304/06195635-065d042f5853432684c3b39cae036013.jpg" /></p>
<p>a2: 1001<span style="color: #ff0000">,</span>ziweiyi<span style="color: #ff0000">,</span>abcd<span style="color: #ff0000">, </span>(末尾有拆分符) 按照' <span style="color: #ff0000">,</span>'拆分开成:1001 ziweiyi abcd</p>
<p>   <img alt="" src="http://images.cnitblog.com/blog/313639/201304/06195804-eed8e4898ed042eb9c04a4258afdfc39.jpg" /></p>
<p>b.字符串拆分:</p>
<p>b1: 1001<span style="color: #ff0000">br</span>ziweiyi<span style="color: #ff0000">br</span>abcd    (末尾无拆分符) 按照“<span style="color: #ff0000">br</span>”拆分开成:1001 ziweiyi abcd</p>
<p>   <img alt="" src="http://images.cnitblog.com/blog/313639/201304/06200300-827e1b4bd719456f96b5643961362821.jpg" /></p>
<p>b2: 1001<span style="color: #ff0000">br</span>ziweiyi<span style="color: #ff0000">br</span>abcd<span style="color: #ff0000">br</span> (末尾有拆分符) 按照“<span style="color: #ff0000"> br</span>”拆分开成:1001 ziweiyi abcd</p>
<p>   <img alt="" src="http://images.cnitblog.com/blog/313639/201304/06200532-a3073b1c27d74e8fbcdce4ad326a3c0c.jpg" /></p>
<p> </p>
<p><a href="http://my.oschina.net/alylee/blog/187504">欢迎大家分享其他的方法,共同学习!</a></p>