Possible Duplicate:
How do I tokenize a string in C++?
Hello I was wondering how I would tokenize a std string with strtok
string line = "hello, world, bye";
char * pch = strtok(line.c_str(),",");
I get the following error
error: invalid conversion from ‘const char*’ to ‘char*’
error: initializing argument 1 of ‘char* strtok(char*, const char*)’
I'm looking for a quick and easy approach to this as I don't think it requires much time
I always use getline
for such tasks.
istringstream is(line);
string part;
while (getline(is, part, ','))
cout << part << endl;
std::string::size_type pos = line.find_first_of(',');
std::string token = line.substr(0, pos);
to find the next token, repeat find_first_of
but start at pos + 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With