the_candyman the_candyman - 3 years ago 161
C++ Question

C++ Tokenize a string with spaces and quotes

I would like to write something in C++ that tokenize a string. To explain what I want, take the following string:

add string "this is a string with spaces!"

This must be splitted as follows:

this is a string with spaces!

Is there a quick and standard-library-based approach?

Answer Source

I guess there is no straight forward approach with standard library. Indirectly following algo will work:

a) search for '\"' with string::find('\"') . If anything found search for next '\"' using string::find('\'',prevIndex), If found use string::substr(). Discard that part from the original string.

b) Now Serach for ' ' character in the same way.

NOTE: you have to iterate through the whole string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download