今天在网上找了一下,发觉有比较多的方法,现在列出来,需要的朋友可以参考一下。
功能最丰富的就是第一种方法了:
1 vector<string> Split(const string& s,const string& match,bool removeEmpty=false,bool fullMatch=false)
2 //参数s为需要肢解的字符串
3 //参数match为肢解匹配字符串
4 //参数removeEmpty为是否删除空字符
5 //参数fullMatch为是否只保留全匹配的字符串
6
7 下面为代码区:
8 #include <string>
9 #include
10
11 namespace Daniweb
12 {
13 using namespace std;
14
15 typedef string::size_type (string::*find_t)(const string& delim,
16 string::size_type offset) const;
17
18 ///
19 /// Splits the string s on the given delimiter(s) and
20 /// returns a list of tokens without the delimiter(s)
21 ///
22 /// The string being split
23 /// The delimiter(s) for splitting
24 /// Removes empty tokens from the list
25 ///
26 /// True if the whole match string is a match, false
27 /// if any character in the match string is a match
28 ///
29 ///
30 vector<string> Split(const string& s,
31 const string& match,
32 bool removeEmpty=false,
33 bool fullMatch=false)
34 {
35 vector<string> result; // return container for tokens
36 string::size_type start = 0, // starting position for searches
37 skip = 1; // positions to skip after a match
38 find_t pfind = &string::find_first_of; // search algorithm for matches
39
40 if (fullMatch)
41 {
42 // use the whole match string as a key
43 // instead of individual characters
44 // skip might be 0. see search loop comments
45 skip = match.length();
46 pfind = &string::find;
47 }
48
49 while (start != string::npos)
50 {
51 // get a complete range [start..end)
52 string::size_type end = (s.*pfind)(match, start);
53
54 // null strings always match in string::find, but
55 // a skip of 0 causes infinite loops. pretend that
56 // no tokens were found and extract the whole string
57 if (skip == 0) end = string::npos;
58
59 string token = s.substr(start, end - start);
60
61 if (!(removeEmpty && token.empty()))
62 {
63 // extract the token and add it to the result list
64 result.push_back(token);
65 }
66
67 // start the next range
68 if ((start = end) != string::npos) start += skip;
69 }
70
71 return result;
72 }
73 }
第二种方法:
1 #include
2 #include <string>
3 #include
4 using namespace std;
5
6 vector<string> splitEx(const string& src, string separate_character)
7 {
8 vector<string> strs;
9
10 int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
11 int lastPosition = 0,index = -1;
12 while (-1 != (index = src.find(separate_character,lastPosition)))
13 {
14 strs.push_back(src.substr(lastPosition,index - lastPosition));
15 lastPosition = index + separate_characterLen;
16 }
17 string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
18 if (!lastString.empty())
19 strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
20 return strs;
21 }
22
23 int _tmain(int argc, _TCHAR* argv[])
24 {
25 string s = "123,456,789,0,888";
26 string del = ",";
27 vector<string> strs = splitEx(s, del);
28 for ( unsigned int i = 0; i < strs.size(); i++)
29 {
30 cout << strs[i].c_str() << endl;
31 }
32 return 0;
33 }
第三种方法:
1 #include
2 #include <string>
3 #include
4
5 using namespace std;
6
7 void split(const string& src, const string& separator, vector<string>& dest)
8 {
9 string str = src;
10 string substring;
11 string::size_type start = 0, index;
12
13 do
14 {
15 index = str.find_first_of(separator,start);
16 if (index != string::npos)
17 {
18 substring = str.substr(start,index-start);
19 dest.push_back(substring);
20 start = str.find_first_not_of(separator,index);
21 if (start == string::npos) return;
22 }
23 }while(index != string::npos);
24 //the last token
25 substring = str.substr(start);
26 dest.push_back(substring);
27 }
28
29
30 int main()
31 {
32 string src = "Accsvr:tcp -h 127.0.0.1 -p /t 20018 ";
33 vector<string> d, s;
34 vector<string>::iterator p, q;
35 split(src,":",d);
36 for(p=d.begin();p!=d.end();++p)
37 {
38 cout << *p << endl;
39 s.clear();
40 split(*p," /t/n",s);
41 for (q=s.begin();q!=s.end();++q)
42 cout << "/t" << *q << endl;
43 }
44 return 0;
45 }
第四种方法:
1 #include
2 #include
3 #include <string.h>
4
5 void split(char *src, const char *separator, char **dest, int *num)
6 {
7 char *pNext;
8 int count = 0;
9 if (src == NULL || strlen(src) == 0) return;
10 if (separator == NULL || strlen(separator) == 0) return;
11 pNext = strtok(src,separator);
12 while(pNext != NULL)
13 {
14 *dest++ = pNext;
15 ++count;
16 pNext = strtok(NULL,separator);
17 }
18 *num = count;
19 }
20
21 int main()
22 {
23 char src[] = "Accsvr:tcp -h 127.0.0.1 -p/n 20018";
24 char *dest[128];
25 char *dest2[128];
26 int num = 0, num2 = 0;
27 int i, j;
28
29 split(src,":",dest,&num);
30
31 for (i=0;i
33 printf("|%s|/n",dest[i]);
34 split(dest[i]," /t/n",dest2,&num2);
35 for (j=0;j
37 printf("|%s|/n",dest2[j]);
38 }
39 }
40 return 0;
41 }
第五种方法:
1 #include
2 #include
3 #include <string.h>
4
5 void split(char *src, const char *separator, char **dest, int *num)
6 {
7 char *pSeparator, *pStart, *pEnd;
8 unsigned int sep_len;
9 int count = 0;
10
11 if (src == NULL || strlen(src) == 0) return;
12
13 pSeparator = (char *)malloc(16);
14 if (pSeparator == NULL) return;
15
16 if (separator == NULL || strlen(separator) == 0) strcpy(pSeparator," ");/* one blank by default */
17 else strcpy(pSeparator,separator);
18
19 sep_len = strlen(pSeparator);
20
21 pStart = src;
22
23 while(1)
24 {
25 pEnd = strstr(pStart, pSeparator);
26 if (pEnd != NULL)
27 {
28 memset(pEnd,'/0',sep_len);
29 *dest++ = pStart;
30 pEnd = pEnd + sep_len;
31 pStart = pEnd;
32 ++count;
33 }
34 else
35 {
36 *dest = pStart;
37 ++count;
38 break;
39 }
40 }
41 *num = count;
42 if (pSeparator != NULL) free(pSeparator);
43 }
44
45 int main()
46 {
47 char src[] = "Accsvr:tcp -h 127.0.0.1 -p 20018";
48 char *dest[128];
49 char *dest2[128];
50 int num = 0, num2 = 0;
51 int i, j;
52
53 split(src,":",dest,&num);
54
55 for (i=0;i
57 printf("|%s|/n",dest[i]);
58 split(dest[i],"/t",dest2,&num2);
59 for (j=0;j
61 printf("|%s|/n",dest2[j]);
62 }
63 }
64 return 0;
65 }