casacore
Public Member Functions | List of all members
casacore::ArrayUtil_global_functions_stringToVector Struct Reference

More...

#include <ArrayUtil.h>

Public Member Functions

Vector< std::string > strToVector (const std::string &string, char delim=',')
 
Vector< std::string > strToVector (const std::string &string, const std::regex &delim)
 

Detailed Description

Split a std::string into its elements.

Review Status

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

Prerequisite

Etymology

stringToVector converts a std::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<std::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"));
Vector< String > stringToVector(const String &string, char delim=',')

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<std::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<std::string> result = stringToVector (source, Regex(" *, *"));

Example


Definition at line 99 of file ArrayUtil.h.

Member Function Documentation

◆ strToVector() [1/2]

Vector<std::string> casacore::ArrayUtil_global_functions_stringToVector::strToVector ( const std::string &  string,
char  delim = ',' 
)

◆ strToVector() [2/2]

Vector<std::string> casacore::ArrayUtil_global_functions_stringToVector::strToVector ( const std::string &  string,
const std::regex &  delim 
)

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