Split a string on newlines in C++
This post will discuss how to split a string on newlines in C++.
1. Using std::getline
A simple solution to split a string on newlines is using the std::getline function. It can be used to extract tokens from the input stream delimited by the newline, 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 <regex> std::vector<std::string> splitString(const std::string& str) { std::vector<std::string> tokens; std::stringstream ss(str); std::string token; while (std::getline(ss, token, '\n')) { tokens.push_back(token); } return tokens; } int main() { std::string str = "C\nC++\nJava"; std::vector<std::string> tokens = splitString(str); for (auto const &token: tokens) { std::cout << token << std::endl; } return 0; } |
Output:
C
C++
Java
2. Using string::find
The std::string::find member function searches a string for the specified character, starting from the specified position. It returns the first occurrence of the specified character and string::npos if it is not found. It can be used as follows to split a string on newlines:
|
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> std::vector<std::string> splitString(const std::string& str) { std::vector<std::string> tokens; std::string::size_type pos = 0; std::string::size_type prev = 0; while ((pos = str.find('\n', prev)) != std::string::npos) { tokens.push_back(str.substr(prev, pos - prev)); prev = pos + 1; } tokens.push_back(str.substr(prev)); return tokens; } int main() { std::string str = "C\nC++\nJava"; std::vector<std::string> tokens = splitString(str); for (auto const &token: tokens) { std::cout << token << std::endl; } return 0; } |
Output:
C
C++
Java
3. Using Boost
Finally, we can use the boost::algorithm::split_regex algorithm offered by the boost library to split the input sequence into tokens, separated by separators. This function is equivalent to C strtok and is available in the header <boost/algorithm/string/regex.hpp>.
|
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 |
#include <iostream> #include <string> #include <vector> #include <regex> #include <boost/regex.hpp> #include <boost/algorithm/string/regex.hpp> std::vector<std::string> splitString(const std::string& str) { std::vector<std::string> tokens; split_regex(tokens, str, boost::regex("(\r\n)+")); return tokens; } int main() { std::string str = "C\nC++\nJava"; std::vector<std::string> tokens = splitString(str); for (auto const &token: tokens) { std::cout << token << std::endl; } return 0; } |
Output:
C
C++
Java
That’s all about splitting a string on newlines in C++.
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 :)