In this article we will see 2 techniques to split a std::string in C++ and return the result in std::vector<std::string> i.e.

  • Splitting a std::string using a char as delimiter.
  • Splitting a std::string using an another std::string as delimiter.

How to Split a std::string using a char as delimiter:

In this we will convert the passed string into a stringstream and from that string stream we will fetch each word using getline method,

But wait a minute, above function splits a string by a character delimiter but what if I want to split it based on another string,

like if first string is “Lets split this line using split functions” then on splitting it with “split” delimiter the result should be,

“Lets”

“this line using”

“functions”

To achieve this we have to write an another split function with std::string as delimiter i.e.

How to split a string by another string as delimiter:

In the above code we will search for the delimiters in the passed string and fetch the sub strings in between them.

Complete executable code is as follows,

Click Here to Subscribe for more Articles / Tutorials like this.