The OpenD Programming Language

split

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

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.

  1. S[] split(S s)
  2. auto split(Range range, Separator sep)
    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
    )
    )
  3. auto split(Range range)

Parameters

range Range

the range to split

sep Separator

a value of the same type as the elements of range or another

Return Value

Type: auto

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

Examples

import std.uni : isWhite;
assert("Learning,D,is,fun".split(",") == ["Learning", "D", "is", "fun"]);
assert("Learning D is fun".split!isWhite == ["Learning", "D", "is", "fun"]);
assert("Learning D is fun".split(" D ") == ["Learning", "is fun"]);
string str = "Hello World!";
assert(str.split == ["Hello", "World!"]);

string str2 = "Hello\t\tWorld\t!";
assert(str2.split == ["Hello", "World", "!"]);
assert(split("hello world") == ["hello","world"]);
assert(split("192.168.0.1", ".") == ["192", "168", "0", "1"]);

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

See Also

std.algorithm.iteration.splitter for a lazy version without allocating memory.

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

Meta