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,


“this line using”


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,

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.