Splitting string into sub-strings is a common task in many applications. In this article, we will discuss various ways to split strings in C++.
1.Using the getline function
The getline function can be used to split strings in C++.
istream& getline (istream& is, string& str, char delim);
- is : Extact string from istream object
- str : extracted line string
- delim : delimiter character
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
void splitstring(const string &str,
char delimiter,
vector<string>& tokens)
{
stringstream ss(str); //Create stringstream object
string word;// to store the token (splitted string)
while(getline(ss,word,delimiter).good())
{
tokens.push_back(word);
}
if (tokens.size()>0)
tokens.push_back(word); // insert last token
}
int main()
{
string str="car,bike,bus,bicycle";
vector<string> vec;
splitstring(str,',',vec);
for(auto s:vec)
{
cout<<s<<"\n";
}
return 0;
}
Output
car
bike
bus
bicycle
To split the string, we will extract individual words/tokens in the string using getline function and inserts extracted tokens into a vector.
2.Using the std::regex_token_iterator function
We can use std::regex_token_iterator function to split string in C++11.
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
void splitstring(string &str,
regex re,
vector<string>& tokens)
{
string word;// to store the token (splitted string)
regex_token_iterator<string::iterator> it( str.begin(), str.end(), re, -1 );
regex_token_iterator<string::iterator> rend;
while( it!=rend )
{
word = *it++;
if (word != str)
tokens.push_back(word);
}
}
int main()
{
string str="car,bike,bus,bicycle";
std::regex regex("\\,");
vector<string> tokens;
splitstring(str,regex,tokens);
for(const auto v :tokens)
{
cout<<v<<"\n";
}
return 0;
}
Output
car
bike
bus
bicycle
3.Using the std::strtok function
A sequence of calls to std::strtok function will give us tokens. Please note that this function uses C strings.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
void splitstring(const string &str,
const char *delimiter,
vector<string>& tokens)
{
string word;// to store the token (splitted string)
char* ptoken = strtok(const_cast<char*>(str.c_str()),delimiter);
while(ptoken!=nullptr)
{
word = string(ptoken);
if (word != str)
tokens.push_back(word);
ptoken = strtok(nullptr,delimiter);
}
}
int main()
{
string str="car,bike,bus,bicycle";
vector<string> tokens;
splitstring(str,",",tokens);
for(const auto v :tokens)
{
cout<<v<<"\n";
}
return 0;
}
Output
car
bike
bus
bicycle
4.Using the std::string::find function
In this method, we will use the string::find function to search the delimiter. Find function will return the position of the separator and we can create tokens from it. We will use string::find and string::substr for tokenization. Here string::npos is used as an error indicator.
#include <iostream>
#include <vector>
using namespace std;
void splitstring(const string &str,
char delimiter,
vector<string> &tokens)
{
string word; // to store the token (splitted string)
size_t offset = 0, count;
size_t pos = str.find(delimiter);
while (pos != string::npos)
{
count = pos - offset;
word = str.substr(offset, count);
tokens.push_back(word);
offset = pos + 1;
pos = str.find(delimiter, pos + 1);
}
if (tokens.size() > 0)
{
word = str.substr(offset);
tokens.push_back(word);
}
}
int main()
{
string str = "car,bike,bus,bicycle";
vector<string> tokens;
splitstring(str, ',', tokens);
for (auto v : tokens)
{
cout << v << "\n";
}
return 0;
}
5.Split string in C++ using boost::split
In this method, we will use boost library for string splitting. we will use the boost::split function to split the string.
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string str("car,bike,bus,bicycle");
vector<string> tokens;
boost::split(tokens, str, boost::is_any_of(","));
for(auto v: tokens )
cout<< v <<"\n";
return 0;
}