c++ implementation of string split [duplicate]
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]);
精彩评论