-1

Possible Duplicates:
C++ split string
How do I tokenize a string in C++?

I'd like to parse a single line 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.

Community
  • 1
  • 1
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134

2 Answers2

1
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

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Mayank
  • 5,454
  • 9
  • 37
  • 60
0

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]);
masay
  • 923
  • 2
  • 17
  • 34