Perfect C++ String Explode Split
Ever wanted the perfect explode or split using the C++ std::string class? Where you can split a string based on a delimiter or separator and store the results in a vector? Here's an easy way to do it!
There were many options available online, but this seemed to be the best way to do it.
int found;
found = str.find_first_of(separator);
while(found != string::npos){
if(found > 0){
results->push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length() > 0){
results->push_back(str);
}
}
Using the std::string function "find_first_of" we get the first instance of separator and store it into found integer.
We then check string::npos and compare with found, so that we make sure our while loop doesn't end until we have reached string::npos (the maximum position of a string index).
If we find an instance, we store it into a std::vector called "results".
Of course we need to update found integer. We also need a solution for the last area, which we push into our results after the while loop.
Here's how to use it:
string thisstring = "Inferno Development is a great programming community and tutorial site";
StringExplode(thisstring, " ", &R);
// vector R now contains each word!
Don't forget to include your headers:
#include <string>
Hopefully this will help many of you with your short small programs dealing with strings. String Explode is a great technique similar to PHP's explode() function and Java's string.split() method.

Anonymous (not verified)
Owhhh,,, nice,,, i'm a newbie
Owhhh,,, nice,,, i'm a newbie haw can i print out them by word ?
does it work if i call R[0] .... ???
Thanks.
Anonymous (not verified)
what about static void
what about
static void split(const string& str, char d, std::list& list){
const char* s(str.c_str());
while(* s ){
const char* item(s);
while( *s && *(s++) != d ); // only mv s fwd if we are not at string end
list.push_back(string(item)); // template over list if needed
}
}
Anonymous (not verified)
????????? i lose my
?????????
i lose my self!
please say how this works!
can be a litle bit shorter example: explode(" ","bla bla");
Anonymous (not verified)
This one seems to work for
This one seems to work for me:
static void split(const std::string& str, char d, std::list& list){
const char* s(str.c_str());
while(*s){
const char* item(s);
while(*s && *s != d) s++; // only mv s fwd if we are not at string end
list.push_back(std::string(item, s-item)); // template over list if needed
if (*s && !*(++s)) list.push_back("");
}
}
void dump(std::list& words) {
std::list::iterator n = words.begin();
while (n != words.end()) {
std::cout << '"' << *n++ << '"' << std::endl;
}
std::cout << std::endl;
}
int main ()
{
std::list words;
split("hello world", ' ', words); dump(words); words.clear();
split(" hello world", ' ', words); dump(words); words.clear();
split(" hello world ", ' ', words); dump(words); words.clear();
split(" hello world ", ' ', words); dump(words); words.clear();
}
will give you:
"hello"
"world"
""
"hello"
"world"
""
"hello"
"world"
""
""
""
"hello"
""
"world"
""
""
As you see, EMPTY words added as empty strings.
Post new comment