Tokenizing a string in C++


In this section, we will see how to tokenize strings in C++. In C we can use the strtok() function for the character array. Here we have a string class. Now we will see how to cut the string using some delimiter from that string.

To use the C++ feature, we have to convert a string to a string stream. Then using getline() function we can do the task. The getline() function takes the string stream, another string to send the output, and the delimiter to stop the stream from scanning.

Let us see the following example to understand how the function is working.

Example Code

 Live Demo

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main() {
   string my_string = "Hello,World,India,Earth,London";
   stringstream ss(my_string); //convert my_string into string stream

   vector<string> tokens;
   string temp_str;

   while(getline(ss, temp_str, ',')){ //use comma as delim for cutting string
      tokens.push_back(temp_str);
   }
   for(int i = 0; i < tokens.size(); i++) {
      cout << tokens[i] << endl;
   }
}

Output

Hello
World
India
Earth
London

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements