Split string in C++ using a delimiter
This post will discuss how to split a string in C++ using a delimiter and construct a vector of strings containing individual strings.
C++ standard library didn’t provide any built-in function for this concrete task. This post provides an overview of some of the available alternatives to accomplish this.
1. Using find_first_not_of() with find() function
We can use a combination of string’s find_first_not_of() and find() functions to split a string in C++, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <string> #include <vector> void tokenize(std::string const &str, const char delim, std::vector<std::string> &out) { size_t start; size_t end = 0; while ((start = str.find_first_not_of(delim, end)) != std::string::npos) { end = str.find(delim, start); out.push_back(str.substr(start, end - start)); } } int main() { std::string s = "C*C++*Java"; const char delim = '*'; std::vector<std::string> out; tokenize(s, delim, out); for (auto &s: out) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
2. Using getline() function
Another good alternative is to use the std::getline function, which extracts characters from the istream object and stores them into a specified stream until the delimitation character is found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <string> #include <vector> #include <sstream> void tokenize(std::string const &str, const char delim, std::vector<std::string> &out) { // construct a stream from the string std::stringstream ss(str); std::string s; while (std::getline(ss, s, delim)) { out.push_back(s); } } int main() { std::string s = "C*C++*Java"; const char delim = '*'; std::vector<std::string> out; tokenize(s, delim, out); for (auto &s: out) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
3. Using std::regex_token_iterator function
Another solution is to use the std::sregex_token_iterator which is a specialization of std::regex_token_iterator<std::string::const_iterator>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <regex> #include <string> int main() { std::string s = "C*C++*Java"; std::regex regex("\\*"); std::vector<std::string> out( std::sregex_token_iterator(s.begin(), s.end(), regex, -1), std::sregex_token_iterator() ); for (auto &s: out) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
4. Use std::strtok function
We can also use the strtok() function to split a string into tokens, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <string> #include <vector> #include <cstring> void tokenize(std::string const &str, const char* delim, std::vector<std::string> &out) { char *token = strtok(const_cast<char*>(str.c_str()), delim); while (token != nullptr) { out.push_back(std::string(token)); token = strtok(nullptr, delim); } } int main() { std::string s = "C*C++*Java"; const char* delim = "*"; std::vector<std::string> out; tokenize(s, delim, out); for (auto &s: out) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
5. Using std::string::find function
Finally, we can use std::string::find algorithm with std::string::substr which is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <string> #include <vector> #include <algorithm> void tokenize(const std::string &s, const char delim, std::vector<std::string> &out) { std::string::size_type beg = 0; for (auto end = 0; (end = s.find(delim, end)) != std::string::npos; ++end) { out.push_back(s.substr(beg, end - beg)); beg = end + 1; } out.push_back(s.substr(beg)); } int main() { std::string s = "C*C++*Java"; const char delim = '*'; std::vector<std::string> out; tokenize(s, delim, out); for (auto &s: out) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
That’s all about splitting a string in C++ using a delimiter.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)