Table of Contents
In this article, we will split a string by comma in C++.
There are several methods available to achieve this. For this, we can create a function and use some inbuilt functions.
Using the for
loop
In this method, we will loop through a string using the for
loop and check each character individually, whether it is a comma or not. For this, we will create a user-defined function.
For example,
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 <bits/stdc++.h> using namespace std; void split_c(string str) { string w = ""; for (auto x : str) { if (x == ',') { cout << w << endl; w = ""; } else { w = w + x; } } cout << w << endl; } int main() { string str = "7,8,9,5"; split_c(str); return 0; } |
Output:
8
9
5
In the above example,
- The
split_c()
function will split the string by commas. - We traverse through each element of the string using the
for
loop. - We check if a character is a comma and if not, then we print it.
Using the getline()
function
The getline()
function can extract some required substring from a given string and store it till it encounters a given delimiter character. This function can split a string by commas.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <vector> #include <string> #include <sstream> #include <iostream> int main() { std::stringstream sstr("7,8,9,5"); std::vector<std::string> v; while(sstr.good()) { std::string substr; getline(sstr, substr, ','); v.push_back(substr); } for (std::size_t i = 0; i < v.size(); i++) std::cout << v[i] << std::endl; } |
Output:
8
9
5
In the above example,
- The
good()
function will run the loop till thesuperstring
object is valid and has not reached its end. - The
getline()
function will read characters and store them in thesubtring
string until the function encounters a comma. - We add this substring to the final vector using the
push_back()
function.
Using the strtok()
function
The strtok()
function splits a string and returns tokens (chunks of string) separated by a delimiter character.
To split a string by commas, we specify the delimiter as a comma. This is the most direct method available.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <cstring> using namespace std; int main() { char str[] = {"7,8,9,5"}; char *ptr; ptr = strtok(str, ","); while (ptr != NULL) { cout << ptr << endl; ptr = strtok (NULL, ","); } return 0; } |
Output:
8
9
5
In the above example,
- A pointer stores the tokens that we split using the
strtok()
function. - We call the function again while displaying to move on to the next token.
- If we do not do this, the code will print the first element infinite times.
- This function does not work with string variables and requires the string to be a
char*
.
Using the std::string::erase()
and std::string::find()
function
We can use the erase()
function and find()
function to split a string by commas in C++. To achieve this, we first set the delimiter as a comma and find the occurrences of this character in the string using the find()
function.
Then, we proceed to erase the substring till the length of the delimiter, which in our case is 1. This will remove the commas, essentially splitting the string.
We implement this in the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { std::string s = "7,8,9,5"; std::string delimiter_char = ","; size_t pos = 0; std::string token; while ((pos = s.find(delimiter_char)) != std::string::npos) { token = s.substr(0, pos); std::cout << token << std::endl; s.erase(0, pos + delimiter_char.length()); } std::cout << s << std::endl; } |
Output:
8
9
5
That’s all about how to split String by comma in C++.