• 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.6/5 (106 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


  • <nobr id="pwjBlLZ"><bdo id="pwjBlLZ"></bdo></nobr>
  • <bdo id="pwjBlLZ"><delect id="pwjBlLZ"></delect></bdo>
  • <bdo id="pwjBlLZ"><delect id="pwjBlLZ"></delect></bdo>
  • <dfn id="pwjBlLZ"></dfn>
  • <dfn id="pwjBlLZ"><delect id="pwjBlLZ"></delect></dfn>
  • <bdo id="pwjBlLZ"></bdo>
  • <bdo id="pwjBlLZ"><delect id="pwjBlLZ"></delect></bdo><dfn id="pwjBlLZ"><delect id="pwjBlLZ"><strike id="pwjBlLZ"></strike></delect></dfn><bdo id="pwjBlLZ"></bdo>
  • <bdo id="pwjBlLZ"></bdo><dfn id="pwjBlLZ"></dfn>
  • <dfn id="pwjBlLZ"></dfn>
  • <dfn id="pwjBlLZ"><delect id="pwjBlLZ"></delect></dfn>
  • <bdo id="pwjBlLZ"></bdo>
  • <dfn id="pwjBlLZ"></dfn>
  • <bdo id="pwjBlLZ"><delect id="pwjBlLZ"></delect></bdo>
  • 6478461606 2018-02-24
  • 4985791605 2018-02-24
  • 5637141604 2018-02-24
  • 282181603 2018-02-24
  • 6217941602 2018-02-24
  • 5076141601 2018-02-24
  • 714281600 2018-02-24
  • 6607141599 2018-02-24
  • 949041598 2018-02-24
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24
  • 9407391594 2018-02-24
  • 6234261593 2018-02-24
  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23