[#absl-StrSplit-011]
= xref:absl.adoc[absl]::StrSplit
:relfileprefix: ../
:mrdocs:

Splits a string into a sequence of substrings identified by `Delimiter`&period; The input is processed sequentially from beginning to end, and each resulting substring is filtered by an optional `Predicate` before inclusion in the result set&period; `StrSplit()` returns a lazy range that preserves the substrings original order and is convertible to the collection type specified by the caller&period;

== Synopsis

Declared in `&lt;absl&sol;strings&sol;str&lowbar;split&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;typename Delimiter&gt;
&sol;&ast; implementation-defined &ast;&sol;::Splitter&lt;&sol;&ast; implementation-defined &ast;&sol;::type, AllowEmpty, std::string&lowbar;view&gt;
StrSplit(
    &sol;&ast; implementation-defined &ast;&sol;::ConvertibleToStringView text,
    Delimiter d);
----

== Description

Optionally, you may pass a `Predicate` to `StrSplit()` indicating whether to include or exclude the resulting element within the final result set&period; (See the overviews for Delimiters and Predicates above&period;)

Example&colon;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v &equals; absl&colon;&colon;StrSplit(&quot;a,b,c,d&quot;, &apos;,&apos;)&semi; &sol;&sol; v&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, v&lsqb;1&rsqb;&equals;&equals; &quot;b&quot;, v&lsqb;2&rsqb;&equals;&equals; &quot;c&quot;, v&lsqb;3&rsqb;&equals;&equals; &quot;d&quot;

You can also provide an explicit `Delimiter` object&colon;

Example&colon;

using absl&colon;&colon;ByAnyChar&semi; std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v &equals; absl&colon;&colon;StrSplit(&quot;a,b&equals;c&quot;, ByAnyChar(&quot;,&equals;&quot;))&semi; &sol;&sol; v&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, v&lsqb;1&rsqb;&equals;&equals; &quot;b&quot;, v&lsqb;2&rsqb;&equals;&equals; &quot;c&quot;

See above for more information on delimiters&period;

By default, empty strings are included in the result set&period; You can optionally include a third `Predicate` argument to apply a test for whether the resultant element should be included in the result set&colon;

Example&colon;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v &equals; absl&colon;&colon;StrSplit(&quot; a , ,,b,&quot;, &apos;,&apos;, SkipWhitespace())&semi; &sol;&sol; v&lsqb;0&rsqb;&equals;&equals; &quot; a &quot;, v&lsqb;1&rsqb;&equals;&equals; &quot;b&quot;

See above for more information on predicates&period;

&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen; StrSplit() Return Types&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;&hyphen;

The `StrSplit()` function adapts the returned collection to the collection specified by the caller (e&period;g&period; `std&colon;&colon;vector` above)&period; The returned collections may contain `std&colon;&colon;string`, `absl&colon;&colon;string&lowbar;view` (in which case the original string being split must ensure that it outlives the collection), or any object that can be explicitly created from an `absl&colon;&colon;string&lowbar;view`&period; This behavior works for&colon;

1) All standard STL containers including `std&colon;&colon;vector`, `std&colon;&colon;list`, `std&colon;&colon;deque`, `std&colon;&colon;set`,`std&colon;&colon;multiset`, &apos;std&colon;&colon;map`, and `std&colon;&colon;multimap&grave;&period; 2) `std&colon;&colon;pair` (which is not actually a container)&period; See below&period; 3) `std&colon;&colon;array`, which is a container but has different behavior due to its fixed size&period; See below&period;

Example&colon;

&sol;&sol; The results are returned as `absl&colon;&colon;string&lowbar;view` objects&period; Note that we &sol;&sol; have to ensure that the input string outlives any results&period; std&colon;&colon;vector&lt;absl&colon;&colon;string&lowbar;view&gt; v &equals; absl&colon;&colon;StrSplit(&quot;a,b,c&quot;, &apos;,&apos;)&semi;

&sol;&sol; Stores results in a std&colon;&colon;set&lt;std&colon;&colon;string&gt;, which also performs &sol;&sol; de&hyphen;duplication and orders the elements in ascending order&period; std&colon;&colon;set&lt;std&colon;&colon;string&gt; a &equals; absl&colon;&colon;StrSplit(&quot;b,a,c,a,b&quot;, &apos;,&apos;)&semi; &sol;&sol; a&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, a&lsqb;1&rsqb;&equals;&equals; &quot;b&quot;, a&lsqb;2&rsqb;&equals;&equals; &quot;c&quot;

&sol;&sol; `StrSplit()` can be used within a range&hyphen;based for loop, in which case &sol;&sol; each element will be of type `absl&colon;&colon;string&lowbar;view`&period; std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&semi; for (const auto sv &colon; absl&colon;&colon;StrSplit(&quot;a,b,c&quot;, &apos;,&apos;)) &lcub; if (sv !&equals; &quot;b&quot;) v&period;emplace&lowbar;back(sv)&semi; &rcub; &sol;&sol; v&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, v&lsqb;1&rsqb;&equals;&equals; &quot;c&quot;

&sol;&sol; Stores results in a map&period; The map implementation assumes that the input &sol;&sol; is provided as a series of key&sol;value pairs&period; For example, the 0th element &sol;&sol; resulting from the split will be stored as a key to the 1st element&period; If &sol;&sol; an odd number of elements are resolved, the last element is paired with &sol;&sol; a default&hyphen;constructed value (e&period;g&period;, empty string)&period; std&colon;&colon;map&lt;std&colon;&colon;string, std&colon;&colon;string&gt; m &equals; absl&colon;&colon;StrSplit(&quot;a,b,c&quot;, &apos;,&apos;)&semi; &sol;&sol; m&lsqb;&quot;a&quot;&rsqb;&equals;&equals; &quot;b&quot;, m&lsqb;&quot;c&quot;&rsqb;&equals;&equals; &quot;&quot; &sol;&sol; last component value equals &quot;&quot;

Splitting to `std&colon;&colon;pair` is an interesting case because it can hold only two elements and is not a collection type&period; When splitting to a `std&colon;&colon;pair` the first two split strings become the `std&colon;&colon;pair` `&period;first` and `&period;second` members, respectively&period; The remaining split substrings are discarded&period; If there are less than two split substrings, the empty string is used for the corresponding `std&colon;&colon;pair` member&period;

Example&colon;

&sol;&sol; Stores first two split strings as the members in a std&colon;&colon;pair&period; std&colon;&colon;pair&lt;std&colon;&colon;string, std&colon;&colon;string&gt; p &equals; absl&colon;&colon;StrSplit(&quot;a,b,c&quot;, &apos;,&apos;)&semi; &sol;&sol; p&period;first &equals;&equals; &quot;a&quot;, p&period;second &equals;&equals; &quot;b&quot; &sol;&sol; &quot;c&quot; is omitted&period;

Splitting to `std&colon;&colon;array` is similar to splitting to `std&colon;&colon;pair`, but for N elements instead of two&semi; missing elements are filled with the empty string and extra elements are discarded&period;

Examples&colon;

&sol;&sol; Stores first two split strings as the elements in a std&colon;&colon;array&period; std&colon;&colon;array&lt;std&colon;&colon;string, 2&gt; a &equals; absl&colon;&colon;StrSplit(&quot;a,b,c&quot;, &apos;,&apos;)&semi; &sol;&sol; a&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, a&lsqb;1&rsqb;&equals;&equals; &quot;b&quot; &sol;&sol; &quot;c&quot; is omitted&period;

&sol;&sol; The second element is empty&period; std&colon;&colon;array&lt;std&colon;&colon;string, 2&gt; a &equals; absl&colon;&colon;StrSplit(&quot;a,&quot;, &apos;,&apos;)&semi; &sol;&sol; a&lsqb;0&rsqb;&equals;&equals; &quot;a&quot;, a&lsqb;1&rsqb;&equals;&equals; &quot;&quot;

The `StrSplit()` function can be used multiple times to perform more complicated splitting logic, such as intelligently parsing key&hyphen;value pairs&period;

Example&colon;

&sol;&sol; The input string &quot;a&equals;b&equals;c,d&equals;e,f&equals;,g&quot; becomes &sol;&sol; &lcub; &quot;a&quot; &equals;&gt; &quot;b&equals;c&quot;, &quot;d&quot; &equals;&gt; &quot;e&quot;, &quot;f&quot; &equals;&gt; &quot;&quot;, &quot;g&quot; &equals;&gt; &quot;&quot; &rcub; std&colon;&colon;map&lt;std&colon;&colon;string, std&colon;&colon;string&gt; m&semi; for (absl&colon;&colon;string&lowbar;view sp &colon; absl&colon;&colon;StrSplit(&quot;a&equals;b&equals;c,d&equals;e,f&equals;,g&quot;, &apos;,&apos;)) &lcub; m&period;insert(absl&colon;&colon;StrSplit(sp, absl&colon;&colon;MaxSplits(&apos;&equals;&apos;, 1)))&semi; &rcub; EXPECT&lowbar;EQ(&quot;b&equals;c&quot;, m&period;find(&quot;a&quot;)&hyphen;&gt;second)&semi; EXPECT&lowbar;EQ(&quot;e&quot;, m&period;find(&quot;d&quot;)&hyphen;&gt;second)&semi; EXPECT&lowbar;EQ(&quot;&quot;, m&period;find(&quot;f&quot;)&hyphen;&gt;second)&semi; EXPECT&lowbar;EQ(&quot;&quot;, m&period;find(&quot;g&quot;)&hyphen;&gt;second)&semi;

WARNING&colon; Due to a legacy bug that is maintained for backward compatibility, splitting the following empty string&lowbar;views produces different results&colon;

absl&colon;&colon;StrSplit(absl&colon;&colon;string&lowbar;view(&quot;&quot;), &apos;&hyphen;&apos;)&semi; &sol;&sol; &lcub;&quot;&quot;&rcub; absl&colon;&colon;StrSplit(absl&colon;&colon;string&lowbar;view(), &apos;&hyphen;&apos;)&semi; &sol;&sol; &lcub;&rcub;, but should be &lcub;&quot;&quot;&rcub;

Try not to depend on this distinction because the bug may one day be fixed&period;

== Return Value

A lazy range of substrings convertible to the caller&apos;s collection&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *text*
| The string to split&period;
| *d*
| The delimiter identifying the split points&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
