Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String tokenizer for CPP String?

I want to use string Tokenizer for CPP string but all I could find was for Char*. Is there anything similar for CPP string?

like image 950
Scarlet Avatar asked Aug 26 '10 09:08

Scarlet


People also ask

What is Tokenizing a string in C++?

Tokenizing a string denotes splitting a string with respect to some delimiter(s).

How do you parse a string in CPP?

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.

What is a string tokenizer?

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.

How does stoi work in C++?

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.


2 Answers

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.

like image 88
sbi Avatar answered Sep 30 '22 12:09

sbi


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.

like image 25
Guillaume Lebourgeois Avatar answered Sep 30 '22 13:09

Guillaume Lebourgeois