Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse (split) a string in C++ using string delimiter (standard C++)

I am parsing a string in C++ using the following:

using namespace std;  string parsed,input="text to be parsed"; stringstream input_stringstream(input);  if (getline(input_stringstream,parsed,' ')) {      // do some processing. } 

Parsing with a single char delimiter is fine. But what if I want to use a string as delimiter.

Example: I want to split:

scott>=tiger 

with >= as delimiter so that I can get scott and tiger.

like image 453
TheCrazyProgrammer Avatar asked Jan 10 '13 19:01

TheCrazyProgrammer


People also ask

How do you split a string with a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

How can I split a string in C?

Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

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.


1 Answers

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.

Example:

std::string s = "scott>=tiger"; std::string delimiter = ">="; std::string token = s.substr(0, s.find(delimiter)); // token is "scott" 
  • 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 substr(size_t pos = 0, size_t n = npos) function returns a substring of the object, starting at position pos and of length npos.


If you have multiple delimiters, after you have extracted one token, you can remove it (delimiter included) to proceed with subsequent extractions (if you want to preserve the original string, just use s = s.substr(pos + delimiter.length());):

s.erase(0, s.find(delimiter) + delimiter.length()); 

This way you can easily loop to get each token.

Complete Example

std::string s = "scott>=tiger>=mushroom"; std::string delimiter = ">=";  size_t pos = 0; std::string token; while ((pos = s.find(delimiter)) != std::string::npos) {     token = s.substr(0, pos);     std::cout << token << std::endl;     s.erase(0, pos + delimiter.length()); } std::cout << s << std::endl; 

Output:

scott tiger mushroom 
like image 102
Vincenzo Pii Avatar answered Oct 14 '22 19:10

Vincenzo Pii