开发者

c++ implementation of string split [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicates:

C++ split string

How do I tokenize a string in C++?

I'd like to parse a single lin开发者_运维问答e of text, which consist of several words separated by blanks. so I need to know the C++ way of split a string by a certain separator. an example line is like below:

aa bb  cc    dd

which means one ore more than one blanks are used as separator in each line of the configuration file. so could any of you can provide some working code to solve my problem? thanks in advance.


string tmp;  
vector<string> out;  
istringstream is("aa bb   cc   dd");  
while(is >> tmp)  
    out.push_back(tmp);

Try with this code. The vector out should contain the tokenized strings


you can use strtok function to split strings

int i=0;
int j=0;
char text[] ="aa bb cc   dd";
char *splittedtext[100]; // have 100 element
splittedtext[0] = strtok(text," ");
while(splittedtext[i] != '\0'){
   ++i;
   splittedtext[i] = strtok(NULL," ");
}
for(j=0;splittedtext[j]!='\0';++j)
   printf("%s",splittedtext[j]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜