原文地址

1 自己用 STL 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <string>
#include <iostream>
using namespace std;

vector<string> split(const string &s, const string &seperator){
vector<string> result;
typedef string::size_type string_size;
string_size i = 0;

while(i != s.size()){
//找到字符串中首个不等于分隔符的字母;
int flag = 0;
while(i != s.size() && flag == 0){
flag = 1;
for(string_size x = 0; x < seperator.size(); ++x)
  if(s[i] == seperator[x]){
  ++i;
  flag = 0;
   break;
  }
}

//找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = 0;
string_size j = i;
while(j != s.size() && flag == 0){
for(string_size x = 0; x < seperator.size(); ++x)
  if(s[j] == seperator[x]){
  flag = 1;
   break;
  }
if(flag == 0)
  ++j;
}
if(i != j){
result.push_back(s.substr(i, j-i));
i = j;
}
}
return result;
}

int main(){
string s = "a,b*c*d,e";
vector<string> v = split(s, ",*"); //可按多个字符来分隔;
for(vector<string>::size_type i = 0; i != v.size(); ++i)
cout << v[i] << " ";
cout << endl;
//输出: a b c d
}

更加简洁的版本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}

2 用 C 语言中的 strtok 函数来进行分割

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string.h>
#include <stdio.h>

int main(){
char s[] = "a,b*c,d";
const char *sep = ",*"; //可按多个字符来分割
char *p;
p = strtok(s, sep);
while(p){
printf("%s ", p);
p = strtok(NULL, sep);
}
printf("\n");
return 0;
}
//输出: a b c d

3 使用 boost 库中的 split 函数

boost 库有很多方法来实现 split,也包含了一个 split 函数,可以直接使用,非常实用而且强大,但是得自己下载 boost 库。使用代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace boost;

void print( vector <string> & v )
{
for (size_t n = 0; n < v.size(); n++)
cout << "\"" << v[ n ] << "\"\n";
cout << endl;
}

int main()
{
string s = "a,b, c ,,e,f,";
vector <string> fields;

cout << "Original = \"" << s << "\"\n\n";

cout << "Split on \',\' only\n";
split( fields, s, is_any_of( "," ) );
print( fields );

cout << "Split on \" ,\"\n";
split( fields, s, is_any_of( " ," ) );
print( fields );

cout << "Split on \" ,\" and elide delimiters\n";
split( fields, s, is_any_of( " ," ), token_compress_on );
print( fields );

return 0;
}

4 更多阅读