How to Split a String by Multiple Delimiters in C++? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, while working with string manipulation we may sometimes need to split a string into substrings based on multiple delimiters. In this article, we will learn how to split a string by multiple delimiters in C++. For Example, Input: string inputString= "Hello,World;This|is.GeeksForGeeks"; string delimiters = ",;.|"; Output: Hello World This is GeeksForGeeksSplit a String into Words by Multiple Delimiters in C++To split a std::string by multiple delimiters, we can use std::string::find_first_of to search the delimiters in a string with the combination of std::string::substr to extract the substrings. ApproachStart with startPos = 0 and endPos = 0, run a loop until endPos is not equal to string::nposUse std::string::find_first_of to find the position of the first occurrence of any delimiter.If found, use std::string::substr to extract the substring between startPos and the delimiter position found by find_first_of and add it to result.If not found, extract the substring from startPos to the end of the string and add it to result also set endPos to string::npos to exit the loop.Repeat the process until the end of the string is reached.C++ Program to Split a String by Multiple DelimitersThe below example demonstrates how we can split a given string by multiple delimiters in C++. C++ // C++ Program to split the string by miltiple delimiters #include <iostream> #include <string> #include <vector> using namespace std; // Function to split a string by multiple delimiters vector<string> splitStringByDelimiters(string& inputString, string& delimiters) { vector<string> result; // Vector to store the split substrings int startPos = 0; int endPos = 0; // Loop until endPos is not equal to string::npos while ((endPos = inputString.find_first_of(delimiters, startPos)) != string::npos) { if (endPos != startPos) { // Checking if the // substring is non-empty result.push_back(inputString.substr( startPos, endPos - startPos)); } startPos = endPos + 1; // Update startPos to the position // after the delimiter } // Extract the substring from startPos to the end of the // string and add it to result if (startPos != inputString.length()) { result.push_back(inputString.substr(startPos)); } return result; } int main() { // string to be splitted string inputString = "Hello,World;This|is.GeeksForGeeks"; // multiple delimiters string delimiters = ",;.|"; // calling the function to split the string using // multiple delimiters vector<string> substrings = splitStringByDelimiters(inputString, delimiters); // Displaying the string after splitiing for (const auto& substring : substrings) { cout << substring << endl; } return 0; } OutputHello World This is GeeksForGeeks Time Complexity: O(n)Auxiliary Space: O(n) , here m is the number of substrings in the result vector. Comment More infoAdvertise with us Next Article How to Split a String by Multiple Delimiters in C++? S shakirradfcz Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-strings C++ String Programs CPP Examples +2 More Practice Tags : CPPcpp-strings Similar Reads How to Split a String by a Delimiter in C++? Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.ExamplesInput: str = "geeks,for,geeks", delimiter = (,)Output: gee 4 min read How to Concatenate Multiple Strings in C++? In C++, to concatenate multiple strings means we need to combine two or more strings into a single string. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!" str2="Geek," str3="Happy Coding." Output: Hello! Geek, Happy Coding.Concatenate Multiple 2 min read How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read How to Handle Multiple String Inputs with Spaces in C++? In C++, strings are a sequence of characters that might contain spaces in many cases but we can read only the input text till whitespace using cin. In this article, we will learn how to handle multiple string inputs with spaces in C++. Example: Input:Enter multiple strings:String1String2Output:You E 2 min read How to Concatenate Multiple C++ Strings on One Line? In C++, strings are used to store a sequence of characters. The combination of multiple strings into a single one is called Concatenation. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!"str2="Geek"str3="Welcome to GfG"Output:Hello! Geek Welcome 1 min read Like