• Articles
  • How to split text into two or more chara
Published by
Aug 10, 2012 (last update: Aug 10, 2012)

How to split text into two or more characters

Score: 3.7/5 (86 votes)
*****
I just like to warn you against to use code from the similar article written by Sean Genge. Don't know, how to write it right, while forum is closed and I can't leave a comment onit.

In general, the one can split the string very easy using STL & C++. You can find two different 'getline' functions in the STL. one - from std::iostream - requires char buffer and isn't really convenient, but other is a public function in the std::string, which allow to define termination character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>
#include <sstream>

int main(int argc, char** arv)
{
    // initialize input stream with data
    std::istringstream ins("this is a    text");
    
    // load words to this container
    std::string out;
    
    // read the words until some data in the input stream
    while (ins.good())
    {
        getline(ins, out, ' '); // tell getline to stop on ' ' character
        if (!out.empty())       // just fast way to remove unnecessary spaces
            std::cout << out << std::endl;
    }
    return 0;
}


Another way is to use ANSI only. A little bit more dangerous, but will be faster. Use 'strtok' function. In the terminology: the words are tokens.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
    // this function will stop on any of these characters
    char const delimiters[] = " \t\n\r";
    
    // initial string to split    
    char ins[] = "this is a    text";
    
    // this pointer will point to the next word after every 'strtok' call
    char *tok = strtok(ins, delimiters);
    
    // if returned pointer is NULL, then there is no more words
    while (0 != tok)
    {
        puts(tok); // print the word
        tok = strtok(NULL, delimiters); // move to the next one
    }
    return 0;
}


Both programs will return:
this
is
a
text

The one can split string to the parts using sscanf-like function, but for that you have to know the type of the items and sometimes their number. Don't invent the code already done by somebody else and proved to be stable. Good luck