Splitting strings...

Started by
3 comments, last by Eight 22 years, 8 months ago
Does anyone have a simple split() function for strings in C++? Non-MFC etc. E.g. string = "Break at spaces" array = string.split(" ") array[1] = "Break" array[2] = "at" array[3] = "spaces" I know it should be a simple enough task to write one, but I''ve yet to get it working properly and I''m really falling behind my schedule now. If anyone wishes to take pity on me and help out, I''d be truly grateful. Cheers.
Advertisement
int split(char*string,char splitter,char**array){ int x,y=0,z=0; for (x=0; string[x]; x++) {  if (string[x] == splitter)  {   y++;   z=0;   continue;  }  array[y][z] = string[x]; } return y; // returns number of strings created} 


Feel free to mix & match to create dynamic arrays, whatever.
Assassin, aka RedBeard. andyc.org
Thanks Assasin.

You''re my new hero.

E
The C library has a function called strtok which does this.

The process of grabbing substrings between delilimiter characters (the characters that effectively separate the substrings which, in this case, is space) is called tokenizing—and the substrings themselves are called tokens.

Edited by - merlin9x9 on July 30, 2001 2:10:11 PM
Yeah but strtok() is a real pain.

For a start, it modifies the original source string and also, to get the result I''m after you still have to write your own routine around it.

Cheers anyway.

This topic is closed to new replies.

Advertisement