Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into words by multiple delimiters

I have some text (meaningful text or arithmetical expression) and I want to split it into words.
If I had a single delimiter, I'd use:

std::stringstream stringStream(inputString); std::string word; while(std::getline(stringStream, word, delimiter))  {     wordVector.push_back(word); } 

How can I break the string into tokens with several delimiters?

like image 676
Sergei G Avatar asked Oct 01 '11 17:10

Sergei G


People also ask

Can split () take multiple arguments?

split() method accepts two arguments. The first optional argument is separator , which specifies what kind of separator to use for splitting the string. If this argument is not provided, the default value is any whitespace, meaning the string will split whenever .

How do I split a string into a list of words?

To convert a string in a list of words, you just need to split it on whitespace. You can use split() from the string class. The default delimiter for this method is whitespace, i.e., when called on a string, it'll split that string at whitespace characters.

How do I split a string with multiple delimiters in SQL?

Using the STUFF & FOR XML PATH function we can derive the input string lists into an XML format based on delimiter lists. And finally we can load the data into the temp table..

How do you pass multiple delimiters in Java?

To split by different delimiters, we should just set all the characters in the pattern. We've defined a test string with names that should be split by characters in the pattern. The pattern itself contains a semicolon, a colon, and a hyphen.


1 Answers

Assuming one of the delimiters is newline, the following reads the line and further splits it by the delimiters. For this example I've chosen the delimiters space, apostrophe, and semi-colon.

std::stringstream stringStream(inputString); std::string line; while(std::getline(stringStream, line))  {     std::size_t prev = 0, pos;     while ((pos = line.find_first_of(" ';", prev)) != std::string::npos)     {         if (pos > prev)             wordVector.push_back(line.substr(prev, pos-prev));         prev = pos+1;     }     if (prev < line.length())         wordVector.push_back(line.substr(prev, std::string::npos)); } 
like image 150
SoapBox Avatar answered Sep 20 '22 14:09

SoapBox