字符串分割
turbo::str_split() 分割字符串
turbo::str_split() 函数提供了一种将字符串拆分为子字符串的简单方法。
str_split() 接受一个要分割的输入字符串、一个用于分割字符串的分隔符(例如逗号 ,),
以及(可选)一个谓词,用于充当过滤器来判断分割元素是否包含在结果集。 str_split() 还将返 回的集合调整为调用者指定的类型。
示例:
// Splits the given string on commas. Returns the results in a
// vector of strings. (Data is copied once.)
std::vector<std::string> v = turbo::str_split("a,b,c", ','); // Can also use ","
// v[0] == "a", v[1] == "b", v[2] == "c"
// Splits the string as in the previous example, except that the results
// are returned as `std::string_view` objects, avoiding copies. Note that
// because we are storing the results within `std::string_view` objects, we
// have to ensure that the input string outlives any results.
std::vector<std::string_view> v = turbo::str_split("a,b,c", ',');
// v[0] == "a", v[1] == "b", v[2] == "c"
str_split() 使用传递的 Delimiter 对象分割字符串。(请参阅下面的 Delimiters。)但是,在许多情况下,
您可以简单地传递字符串文字作为分隔符(它将隐式转换为 turbo::ByString 分隔符)。
示例:
// By default, empty strings are *included* in the output. See the
// `turbo::SkipEmpty()` predicate below to omit them{#stringSplitting}.
std::vector<std::string> v = turbo::str_split("a,b,,c", ',');
// v[0] == "a", v[1] == "b", v[2] == "", v[3] = "c"
// You can also split an empty string
v = turbo::str_split("", ',');
// v[0] = ""
// The delimiter need not be a single character
std::vector<std::string> v = turbo::str_split("aCOMMAbCOMMAc", "COMMA");
// v[0] == "a", v[1] == "b", v[2] == "c"
// You can also use the empty string as the delimiter, which will split
// a string into its constituent characters.
std::vector<std::string> v = turbo::str_split("abcd", "");
// v[0] == "a", v[1] == "b", v[2] == "c", v[3] = "d"