I want to use string Tokenizer for CPP string but all I could find was for Char*. Is there anything similar for CPP string?
Tokenizing a string denotes splitting a string with respect to some delimiter(s).
You can use the std::string::find() function to find the position of your string delimiter, then use std::string::substr() to get a token. The find(const string& str, size_t pos = 0) function returns the position of the first occurrence of str in the string, or npos if the string is not found.
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.
What do you mean by "token"? If it's something separated by any whitespace, the string streams is what you want:
std::istringstream iss("blah wrxgl bxrcy")
for(;;) {
std::string token;
if(!(iss>>token)) break;
process(token);
}
if(!iss.eof()) report_error();
Alternatively, if your looking for a a certain single separating character, you can replace iss>>token
with std::getline(iss,token,sep_char)
.
If it's more than one character that can act as a separator (and if it's not whitespaces), a combinations of std::string::find_first()
and std::string::substr()
should do.
You can do as said by chubsdad or use boost tokenizer : http://www.boost.org/doc/libs/1_44_0/libs/tokenizer/tokenizer.htm
Doing it by yourself is not so complicated if you're affraid by Boost.
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