casa
$Rev:20696$
|
Split a String into its elements. More...
#include <ArrayUtil.h>
Public Member Functions | |
Vector< String > | stringToVector (const String &string, char delim= ',') |
Vector< String > | stringToVector (const String &string, const Regex &delim) |
Split a String into its elements.
stringToVector converts a String to a Vector of Strings.
The function stringToVector splits a string into its elements using the given delimiter and returns them in a Vector<String>
. The default delimiter is a comma (,). It is very useful when using a function taking a vector of strings as shown in the example.
A more advanced way of splitting a string is by using a regular expression as delimiter. It makes it, for example, possible to treat whitespace around a comma as part of the delimiter (as shown in an example below).
A string with length 0 results in a zero-length vector.
As shown in the example, the function stringToVector makes passing a Vector of Strings far easier.
someFunction (stringToVector ("abc,def ,,gh"));
This results in a vector with 4 elements containing the values "abc", "def ", "", and "gh". The vector is passed to someFunction. This is far easier than having to do it as:
Vector<String> vector(4); vector(0) = "abc"; vector(1) = "def "; vector(2) = ""; vector(3) = "gh"; someFunction (vector);
The following example shows how to use a delimiter consisting of a comma surrounded by possible whitespace.
Vector<String> result = stringToVector (source, Regex(" *, *"));
Definition at line 101 of file ArrayUtil.h.
Vector<String> casa::ArrayUtil_global_functions_stringToVector::stringToVector | ( | const String & | string, |
char | delim = ',' |
||
) |
Vector<String> casa::ArrayUtil_global_functions_stringToVector::stringToVector | ( | const String & | string, |
const Regex & | delim | ||
) |