Daily Archives: June 13, 2014

C++ || Snippet – Custom “strtok” Function To Split A String Into Tokens Using Multiple Delimiters

The following is a custom “strtok” function which splits a string into individual tokens according to delimiters. So for example, if you had an string full of punctuation characters and you wanted to remove them, the following function can do that for you.

This function works by accepting an std::string containing the text to be broken up into smaller std::strings (tokens), aswell as another std::string that contains the delimiter characters. After the parse is complete, it returns a vector object containing all the found tokens in the string.

The code demonstrated on this page is different from the cstring strtok function in that this implementation works for C++ only.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

The original string: - This, is. a sample string? 3!30$' 19: 68/, LF+, 1, 1

The tokens:
This
is
a
sample
string
3
30
19
68
LF
1
1