Skip to content

Rose::StringUtility::split

Splits strings into parts.

Synopsis

Declared in <src/Rose/StringUtility/SplitJoin.h>

[[visibility]]
std::vector<std::string>
split(
    std::string const& separator,
    std::string const& str,
    size_t maxparts = UNLIMITED,
    bool trim_white_space = false);

Description

Find all non‐overlapping occurrences the specified separator string by greedily scanning from left to right in the input string, str. The input string is then logically chopped into parts at each separator position and the parts are assembled into the return value. Only up to maxparts‐1 occurrences of the separator string are found, and any remaining occurrences are not treated specially. For instance, if maxparts is two then at most one separator is found and at most two substrings are returned. Separators at positions that would result in empty substrings being returned are not treated specially‐‐empty substrings can be returned. This occurs when a separator is found at the beginning or end of a string or two separators are adjacent. The C++ library already has other functions for removing empty strings from a list. If trim_white_space is true then white space is removed from the beginning and end of each returned substring and resulting empty substrings are not removed from the return value. The first few arguments are in the same order as for Perl's "split" operator.

@{

Return Value

List of substrings.

Parameters

Name

Description

separator

Separator string.

str

Input string.

maxparts

Maximum number of parts.

trim_white_space

Trim whitespace in parts.

Created with MrDocs