
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
Advertisements