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

Related Posts

  • C++ wait seconds
    06 November

    How to wait for seconds in C++?

    Table of ContentsWait For Seconds Using The sleep () Function in C++Wait For Seconds Using Thread In C++Wait for seconds using sleep_for() function in C++Wait for seconds using sleep_until() function in C++Wait for seconds using delay() function in C++Wait for seconds using the wait() function in CWait For Seconds Using Timer in C++Conclusion There are […]

  • C++ copy array
    06 November

    Copy Array in C++

    Table of ContentsShallow Copy And Deep Copy In C++How To Copy Array Elements Using Library Functions in C++?How To Copy Array Elements Using The copy() Function in C++?How To Copy Array Elements Using The copy_n() Function in C++?How To Copy Array Elements Using The copy_if() Function in C++?How To Copy Array Elements Using The copy_backward() […]

  • C++ read file into array
    06 November

    Read file into array in C++

    Table of ContentsHow to declare an array in C++?How To Open A File in C++?C++ Libraries For File HandlingOpening File in C++Open A File in C++ Using The open() FunctionProgram To Read File Into Array In C++Explanation For The CodeTakeaways C++ is a powerful language that provides us with ways to read the data from […]

  • Read CSV file in C++
    01 November

    How to read CSV file in C++?

    Table of ContentsWhat is a CSV File? How To Read A CSV File In C++?C++ Code To Read A CSV FileHow To Read A CSV File Using C-Based Approach?Conclusion CSV files or Comma Separated Value Files are simple yet special files that we use to store data. Here, each value is separated by a comma, and […]

  • Count lines in file in C++
    01 November

    Count lines in file in C++

    Table of ContentsHow To Count Lines In A File In C++?C++ Program To Count Lines In A FileHow To Count Lines In A File In C++ Using C-style Approach?C++ Program to Count Lines In A File using C-style approachConclusion We often store our data into files when we need the data to be persistent and […]

  • Get Number of Elements in Array in C++
    08 October

    Get Number of Elements in Array in C++

    Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.