How to Split String in C++
-
Use the
std::string::find
andstd::string::erase
Functions to Split String in C++ -
Use
std::getline
anderase-remove
Idiom to Split String in C++ -
Use
std::istringstream
Withstd::copy
andstd::istream_iterator
to Split String in C++
This article will explain several methods of how to split a string in C++.
Use the std::string::find
and std::string::erase
Functions to Split String in C++
The find
and erase
functions are built-in members of the std::string
class, and they can be combined to split the text into tokens delimited by the given characters. This method can be utilized to split a string by any user-declared substring or a single character. Still, the following example code demonstrates the scenario when the delimiter is a
. find
method returns a position of the substring if it is found or string::npos
if not found. Note that we need to delete the processed part of the string
plus the delimiter. Thus, the erase()
function is called to deal with this task.
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
int main() {
string text =
"Vivamus quis sagittis diam. "
"Cras accumsan, dui id varius "
"vitae tortor.";
string delimiter = "a";
vector<string> words{};
size_t pos;
while ((pos = text.find(delimiter)) != string::npos) {
words.push_back(text.substr(0, pos));
text.erase(0, pos + delimiter.length());
}
for (const auto &str : words) {
cout << str << endl;
}
return EXIT_SUCCESS;
}
Output:
Viv
mus quis s
gittis di
m. Cr
s
ccums
n, dui id v
rius vit
Use std::getline
and erase-remove
Idiom to Split String in C++
A similar method to solve the given problem is to use the std::getline
function, which also can extract substrings between the delimiter that the user specifies. The next sample code splits the text on each space character and stores the extracted strings into a std::vector
container. Note though, there is an erase-remove
idiom used to delete any punctuation characters from the strings before they are stored.
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
int main() {
string text =
"Vivamus quis sagittis diam. "
"Cras accumsan, dui id varius "
"vitae tortor.";
vector<string> words{};
char delimiter = ' ';
istringstream sstream(text);
string word;
while (std::getline(sstream, word, delimiter)) {
word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
words.push_back(word);
}
for (const auto &str : words) {
cout << str << endl;
}
return EXIT_SUCCESS;
}
Output:
Vivamus
quis
sagittis
diam
Cras
accumsan
dui
id
varius
vitae
tortor
Use std::istringstream
With std::copy
and std::istream_iterator
to Split String in C++
Alternatively, one could initialize the std::istringstream
object with the text that needs to be split and traverse it with std::istream_iterator
. Note that this method can only split strings by spaces which is the default delimiter for the istream_iterator
. Finally, we need to copy the extracted strings to the vector
container, which is done using the std::copy
algorithm.
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
int main() {
string text =
"Vivamus quis sagittis diam. "
"Cras accumsan, dui id varius "
"vitae tortor.";
vector<string> words{};
char delimiter = ' ';
istringstream iss(text);
copy(std::istream_iterator<string>(iss), std::istream_iterator<string>(),
std::back_inserter(words));
for (const auto &str : words) {
cout << str << endl;
}
return EXIT_SUCCESS;
}
Output:
Vivamus
quis
sagittis
diam.
Cras
accumsan,
dui
id
varius
vitae
tortor.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn FacebookRelated Article - C++ String
- How to Capitalize First Letter of a String in C++
- How to Find the Longest Common Substring in C++
- How to Find the First Repeating Character in a String in C++
- How to Compare String and Character in C++
- How to Get the Last Character From a String in C++
- How to Remove Last Character From a String in C++