View source code
Display the source code in std/array.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.array.split

Eagerly splits range into an array, using sep as the delimiter.

S[] split(S) (
  S s
) pure @safe
if (isSomeString!S);

auto split(Range, Separator) (
  Range range,
  Separator sep
)
if (isForwardRange!Range && (is(typeof(ElementType!Range.init == Separator.init)) || is(typeof(ElementType!Range.init == ElementType!Separator.init)) && isForwardRange!Separator));

auto split(alias isTerminator, Range) (
  Range range
)
if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(range.front))));

When no delimiter is provided, strings are split into an array of words, using whitespace as delimiter. Runs of whitespace are merged together (no empty words are produced).

The range must be a forward range. The separator can be a value of the same type as the elements in range or it can be another forward range.

Parameters

NameDescription
s the string to split by word if no separator is given
range the range to split
sep a value of the same type as the elements of range or another
isTerminator a predicate that splits the range when it returns true.

Returns

An array containing the divided parts of range (or the words of s).

See Also

splitter for a lazy version without allocating memory.

splitter for a version that splits using a regular expression defined separator.

Example

import std.uni : isWhite;
writeln("Learning,D,is,fun".split(",")); // ["Learning", "D", "is", "fun"]
writeln("Learning D is fun".split!isWhite); // ["Learning", "D", "is", "fun"]
writeln("Learning D is fun".split(" D ")); // ["Learning", "is fun"]

Example

string str = "Hello World!";
writeln(str.split); // ["Hello", "World!"]

string str2 = "Hello\t\tWorld\t!";
writeln(str2.split); // ["Hello", "World", "!"]

Example

writeln(split("hello world")); // ["hello", "world"]
writeln(split("192.168.0.1", ".")); // ["192", "168", "0", "1"]

auto a = split([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [2, 3]);
writeln(a); // [[1], [4, 5, 1], [4, 5]]

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.