| Troy (21) | |
| 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
|
|
| kooth (570) | |||
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:
I haven't tested this! | |||
|
Last edited on
|
|||
| Athar (3764) | |
|
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. | |
|
|
|
| bigearsbilly (143) | |
|
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
|
|