Home | Projects | Notes > C++ Programming > String Manipulation
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.
For example,
xxxxxxxxxx
241
2
3
4
5using namespace std;
6
7int main(int argc, char *argv[])
8{
9 string str = "Hello, world! My name is Kyungjae Lee.";
10 stringstream ss(str); // convert str into string stream
11
12 vector<string> tokens;
13 string tok;
14
15 // tokenize using space as a delimiter
16 while (getline(ss, tok, ' '))
17 tokens.push_back(temp_str);
18
19 // print tokens
20 for (auto tok : tokens)
21 cout << tok << endl;
22
23 return 0;
24}
xxxxxxxxxx
71Hello,
2world!
3My
4name
5is
6Kyungjae
7Lee.