Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I tokenize a string in C++?

Java has a convenient split method:
String str = "The quick brown fox";
String[] results = str.split(" ");


Is there an easy way to do this in C++?
by

3 Answers

espadacoder11
The Boost tokenizer class can make this sort of thing quite simple:

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;

int main(int, char
)
{
string text = "token, test string";

char_separator<char> sep(", ");
tokenizer< char_separator<char> > tokens(text, sep);
BOOST_FOREACH (const string& t, tokens) {
cout << t << "." << endl;
}
}

Updated for C++11:

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;

int main(int, char)
{
string text = "token, test string";

char_separator<char> sep(", ");
tokenizer<char_separator<char>> tokens(text, sep);
for (const auto& t : tokens) {
cout << t << "." << endl;
}
}
sandhya6gczb
Example of tokenizer

#include <vector>
#include <string>
using namespace std;

vector<string> split(const char str, char c = ' ')
{
vector<string> result;

do
{
const char *begin = str;

while(*str != c && *str)
str++;

result.push_back(string(begin, str));
} while (0 !=
str++);

return result;
}
pankajshivnani123
Another quick way is to use getline. Something like:

stringstream ss("bla bla");
string s;

while (getline(ss, s, ' ')) {
cout << s << endl;
}

If you want, you can make a simple split() method returning a vector<string>, which is really useful.

Login / Signup to Answer the Question.