Splitting strings...
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.
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement