Orxonox  0.0.5 Codename: Arcturus
Classes | Namespaces
SubString.h File Reference

A helper class to split a string into several tokens. More...

#include "UtilPrereqs.h"
#include <vector>
#include <string>

Go to the source code of this file.

Classes

class  orxonox::SubString
 A class that splits a string into multiple tokens using different options. More...
 

Namespaces

 orxonox
 Die Wagnis Klasse hat die folgenden Aufgaben:
 

Detailed Description

A helper class to split a string into several tokens.

The class SubString can be used to split an std::string into multiple tokens, using a delimiter. SubString allows different options, for example to remove whitespaces around the delimiters or different safe-mode chars, like quotation marks and braces.

You can access the tokens of the SubString using the [] operator like an array. SubString also supports to join the tokens (or a subset of the tokens) again using join(). It's even possible to get a subset of the SubString as another SubString using subSet().

Example:

std::string text = "This is a test, \"Hello \\\" World\" and vector {1, 2, 3}";
SubString tokens(text, SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true, '\0');
for (size_t i = 0; i < tokens.size(); ++i)
orxout() << i << ": " << tokens[i] << endl;

The output of this code is:

The string was split using the delimiter " ". A string between quotation mark is not split, the same holds for strings between '{' and '}'. Note how the quotation marks and the braces were removed from the tokens, because the corresponding argument is 'true'.

Also note that the comma after "test" in token 3 is still there - it is neither part of the delimiters SubString::WhiteSpaces nor part of the delimiterNeighbours parameter, so it remains a part of the token.