Archived

This topic is now archived and is closed to further replies.

Eight

Splitting strings...

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites