You are here:

C++/string tokenizer

Advertisement


Question
i have a string
string a;
str = "apple|mango|grape|banana|strawberry"
if i use
a = strtok(str, "|");
cout<<a<<endl;

my output is apple;
how to use it for extracting all other tokens and store them in separate variables

Answer
You can make a call again passing NULL as the first argument to strtok, as in:
http://www.cplusplus.com/reference/clibrary/cstring/strtok.html

You can also use C++ member: find_first_of etc in place of strtok. See www.sgi.com/tech/stl/basic_string.html

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.