casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions
casa::ArrayUtil_global_functions_stringToVector Struct Reference

Split a String into its elements. More...

#include <ArrayUtil.h>

List of all members.

Public Member Functions

Vector< StringstringToVector (const String &string, char delim= ',')
Vector< StringstringToVector (const String &string, const Regex &delim)

Detailed Description

Split a String into its elements.

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25
Test programs:
tArrayUtil

Prerequisite

Etymology

stringToVector converts a String to a Vector of Strings.

Synopsis

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.

Motivation

As shown in the example, the function stringToVector makes passing a Vector of Strings far easier.

Example

    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(" *, *"));

Example

Definition at line 101 of file ArrayUtil.h.


Member Function Documentation


The documentation for this struct was generated from the following file: