Split String by comma in C++

Split String by comma in C++

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,

Output:

7
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,

Output:

7
8
9
5

In the above example,

  • The good() function will run the loop till the superstring object is valid and has not reached its end.
  • The getline() function will read characters and store them in the subtring 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,

Output:

7
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.

Output:

7
8
9
5

That’s all about how to split String by comma in C++.

Was this post helpful?

Related Posts

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++ConclusionWas this post helpful? C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusionWas this post helpful? An […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.