String module

split

procedure · 1 overloadSource

The pieces of s between each separator (a string or a byte).

ExampleOpen in playground ↗
parts := split("a,b,c", ",");
assert(parts.count == 3 && parts[1] == "b");

Empty pieces are kept: ;a;; split at ; gives four pieces, and an empty string gives one empty piece. The array is allocated with the context allocator and the caller frees it with array_free; the pieces point into s and are not freed.

ExampleOpen in playground ↗
parts := split(";a;;", #char ";");
defer array_free(parts);
assert(parts.count == 4 && parts[0] == "" && parts[1] == "a" && parts[3] == "");
split :: (s: string, separator: $T/ .[string, u8]) -> [] string

The pieces of s between each separator, as views into s, in a new array that the caller frees. A separator at either end, or two in a row, gives an empty piece.

Parameters

s: string
separator: $T/ .[string, u8]
a string, or a single byte.

Returns

[] string
ExampleOpen in playground ↗
pieces := split("a,b,,c", #char ",");
defer array_free(pieces);
assert(pieces.count == 4 && pieces[2] == "");

See also