C++ Splitting strings into tokens.

How would I go about splitting a string into tokens? I want to be able to input a string such as "cmd www http://www.google.com" and it would parse "cmd" as "command" and "www" as a parameter for using the web browser, and the last parameter as the url. I'm making a program that the user can input those types of things and it runs them. Kinda like the command prompt I guess.
Last edited on
There are very many ways to do this. One way that comes to mind is using the stringstream class to parse your input. If you have the user input the data as one string, you could do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;


void ParseString( const string& Input, vector< string >& tokens )
    {
    string
        Buffer;
    

    stringstream ss( Input );

    while( ss >> Buffer )
        tokens.push_back( Buffer );

    }
        /*    ParseString()    */

int main( int argc, char** argv )
    {
    char InputBuffer[ 256 ];

    string
        Cmd = "",
        InputString = "",
        Parameter = "",
        URL = "";

    vector< string > Tokens;


    cout << "Enter the string:" << endl;

    cin.getline( InputBuffer, sizeof( InputBuffer ) );

    InputString = InputBuffer;

    ParseString( InputString, Tokens );

    vector< string >::iterator itr;

    for( int i = 0, itr = Tokens.begin(); itr != Tokens.end(); itr++, i++ )
        {
        ThisString = *itr;

        switch( i )
            {
            case 0:    /*    cmd    */
                Cmd = ThisString.data();

                break;

            case 1:    /*    parameter    */
                Parameter = ThisString.data();

                break;

            case 2:    /*    URL    */
                URL = ThisString.data();

                break;

            default:
                 break;

            }    /*    switch( i )    */

        }    /*    for( itr != Tokens.end() )    */

    /*    Now run your command:    */

    }
        /*    main()    */



I haven't tested this!
Last edited on
This is generally done using a split function.
Boost has one:
http://www.boost.org/doc/libs/1_47_0/doc/html/string_algo/usage.html#id2896235

It's rather awkward to use, so I don't personally recommend it. Rather, create your own variant.
I guess you'll want to use regular expressions rather than straight tokens?


pcre++ is widespread and it has an easy split() which splits to a vector<string>



http://www.daemon.de/PcreSamples
Last edited on
Topic archived. No new replies allowed.