字符串分割方法代码

一、需求:把字符串按照给定拆分符(字符或字符串)拆分开:

例如:

 a.字符拆分:

 a1:  1001,ziweiyi,abcd  (末尾无拆分符)  按照' ,'拆分开成:1001  ziweiyi  abcd

 a2:  1001,ziweiyi,abcd, (末尾有拆分符)  按照' ,'拆分开成:1001  ziweiyi  abcd

 

 b.字符串拆分:

 b1:  1001brziweiyibrabcd    (末尾无拆分符)  按照“br”拆分开成:1001  ziweiyi  abcd

 b2:  1001brziweiyibrabcdbr (末尾有拆分符)  按照“ br”拆分开成:1001  ziweiyi  abcd

 

二、上代码:这里提供两个可用拆分函数,请根据字喜好选择使用

方法一:

 1 void SplitString(char* src,const char* separator, vector<string>& destVector)
 2 {
 3 
 4     destVector.clear();
 5     if(0==strlen(separator))
 6     {
 7         destVector.push_back(string(src));
 8         return ;
 9     }
10 
11     char *first,*second;
12     char tmp[50];
13     first = src;
14     while(first)
15     {
16         memset(tmp,'\0',sizeof(tmp));
17         second = strstr(first,separator);
18         if (NULL==second)
19         {
20             strncpy(tmp,first,strlen(src));
21         }
22         else
23         {
24             strncpy(tmp,first,second-first);
25         }
26 
27         if (strlen(tmp)!=0)
28         {
29             destVector.push_back(string(tmp));
30         }
31         first = second +strlen(separator);
32 
33         if (NULL==second)
34             break;
35     }
36 
37 }

 方法二:

 1 int SplitString(const string src, string separator, vector<string>& destVector)
 2 {
 3     string temp, SrcTemp;
 4     int nPos = 0, nResultCount = 0;
 5     bool bAddEmpty = false;
 6     destVector.clear();
 7     SrcTemp = src;
 8 
 9     if(separator.empty())
10     {
11         destVector.push_back(SrcTemp);
12         return 1;
13     }
14 
15     do
16     {
17         nPos = SrcTemp.find(separator);
18 
19         if(nPos != string::npos)
20         {
21             if(nPos == 0)
22             {
23                 SrcTemp = SrcTemp.substr(nPos + separator.length(),
24                     SrcTemp.length() - nPos - separator.length());
25                 continue;
26             }
27             else if(nPos > 0)
28             {
29                 temp = SrcTemp.substr(0, nPos);
30 
31                 SrcTemp = SrcTemp.substr(nPos + separator.length(),
32                     SrcTemp.length() - nPos - separator.length());
33                 if (temp.length() > 0)
34                 {
35                     destVector.push_back(temp);
36                     nResultCount++;
37                 }
38                 else if (bAddEmpty)
39                 {
40                     destVector.push_back(temp);
41                     nResultCount++;
42                 }
43 
44             }
45         }
46         else
47         {
48             if(nResultCount == 0)
49             {
50                 if (SrcTemp.length() > 0)
51                 {
52                     destVector.push_back(SrcTemp);
53                     nResultCount++;
54                 }
55                 else if (bAddEmpty)
56                 {
57                     destVector.push_back(SrcTemp);
58                     nResultCount++;
59                 }
60                 SrcTemp = "";
61             }
62             else if(nResultCount > 0)
63             {
64                 if (SrcTemp.length() > 0)
65                 {
66                     destVector.push_back(SrcTemp);
67                     nResultCount++;
68                 }
69                 else if (bAddEmpty)
70                 {
71                     destVector.push_back(SrcTemp);
72                     nResultCount++;
73                 }
74                 SrcTemp = "";
75             }
76             else
77                 return -1;
78         }
79     } while(!SrcTemp.empty());
80 
81     return nResultCount;
82 }

三、测试main

 1 void main()
 2 {
 3     vector<string> vecTar;
 4     char szSrc[]="1001,ziweiyi,abcd";
 5     SplitString(szSrc, ",", vecTar);//方法一
 6     for (int i = 0; i < vecTar.size();i ++)
 7     {
 8         printf("【%s】 SplitString: %s\n",szSrc,vecTar[i].c_str());
 9     }
10     printf("方法1,end.\n\n");
11 
12     string strSrc="1001,ziweiyi,abcd";
13     int nRet = SplitString(strSrc, ",", vecTar);//方法二
14     for (int i = 0; i < nRet;i ++)
15     {
16         printf("【%s】 SplitString: %s\n",strSrc.c_str(),vecTar[i].c_str());
17     }
18     printf("方法2,end.\n\n");
19 
20 }

这里指给出了最常见的案例(需求a1的情况),其他的可以自己测试,本人已测试过。

贴出测试效果,更明白:

   a1:  1001,ziweiyi,abcd  (末尾无拆分符)  按照' ,'拆分开成:1001  ziweiyi  abcd

   

 a2:  1001,ziweiyi,abcd(末尾有拆分符)  按照' ,'拆分开成:1001  ziweiyi  abcd

   

 b.字符串拆分:

 b1:  1001brziweiyibrabcd    (末尾无拆分符)  按照“br”拆分开成:1001  ziweiyi  abcd

   

 b2:  1001brziweiyibrabcdbr (末尾有拆分符)  按照“ br”拆分开成:1001  ziweiyi  abcd

   

 

欢迎大家分享其他的方法,共同学习!

 

posted @ 2013-04-06 20:11  子韦一  Views(847)  Comments(0Edit  收藏  举报