Javatpoint Logo
Javatpoint Logo

How to Split a String by Multiple Delimiters in C++?

Splitting the string in programming is a very common case. In solving many problems or optimizing the programs, programmers come across splitting the string. It can be done in many ways in C++. The different approaches will give different time and space complexity. This article will give some possible ways to split the string using the delimiters.

The delimiters are the characters used to mark the boundaries in the sentence. These are helpful while reading certain texts. Sometimes, these are used to express some meaning to the sentences. Some of the delimiters are space, comma, question mark, colon, semicolon, hyphen, forward slash, underscore, tab, exclamation marks, etc.

Example 1:

Let's take some examples to demonstrate how to split a string by multiple delimiters.

String: Hurry! We won the match, It is an achievement.

Delimiters: !,.

Output:

Hurry

We won the match

It is an achievement

Example 2:

String: Have a nice day

Delimiter: space

Output:

Have

a

nice

day

Below are some of the different ways to split the strings.

  • Using std::istringstream and std::getline():

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::istringstream() and std::getline() methods in C++.

Output:

How to Split a String by Multiple Delimiters in C++

Explanation:

This program will split the static string by using the delimiters, which are stored in the vector. Here, the isstringstream is used to iterate through the each word and uses getline() function to split the word.

  • Using std::regex

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::regex() method in C++.

Output:

How to Split a String by Multiple Delimiters in C++

Explanation:

This program uses regular explanation to split the string in C++. Usually, the delimiters are stored in vector a, and regex is used to identify the delimiters in the string and for splitting.

  • Using std::string::find_first_of() and std::string::substr():

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::string::find_first_of() and std::string::substr() methods in C++.

Output:

How to Split a String by Multiple Delimiters in C++

Explanation:

This program is another way to split the string. Here, we iterate through the input.find, the positions of the delimiters, and then extract the tokens from the string. After that, we push the sub-strings into the resultant vector, where the delimiters are removed.

  • Using Boost.Tokenizer:

Let's take an example to demonstrate how to split a string by multiple delimiters using Boost.Tokenizer() method in C++.

Output:

How to Split a String by Multiple Delimiters in C++

Explanation:

This program uses the Boost.Tokenizer to split the input here is the static string by delimiters specified in the delimiters vector. First, it creates the tokenizer object, and then uses that object and gives gives the string after splitting with multiple delimiters.

  • Splitting the string by using space as delimiter

Let's take an example to demonstrate how to split a string by multiple delimiters using space as delimiter in C++.

Output:

How to Split a String by Multiple Delimiters in C++

Conclusion:

In conclusion, this article will provide some of the ways to split the strings in an efficient way. These programs are related to C++. C++ has no particular in-built function to split the string using a delimiter. So, these methods are helpful while dealing with strings in C++ language programming.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA