flatten

Flattens a dynamic array of strings by splitting elements containing more than one value (as separated by a separator string) into separate elements.

private @safe
flatten
(
const string[] arr
,
const string separator = " "
)

Parameters

arr string[]

A dynamic string[] array.

separator string

Separator, defaults to a space string (" ").

Return Value

Type: auto

A new array, with any elements previously containing more than one separator-separated entries now in separate elements.

Examples

import std.conv : to;

{
    auto arr = [ "a", "b", "c d e   ", "f" ];
    arr = flatten(arr);
    assert((arr == [ "a", "b", "c", "d", "e", "f" ]), arr.to!string);
}
{
    auto arr = [ "a", "b", "c,d,e,,,", "f" ];
    arr = flatten(arr, ",");
    assert((arr == [ "a", "b", "c", "d", "e", "f" ]), arr.to!string);
}
{
    auto arr = [ "a", "b", "c dhonk  e ", "f" ];
    arr = flatten(arr, "honk");
    assert((arr == [ "a", "b", "c d", "e", "f" ]), arr.to!string);
}
{
    auto arr = [ "a", "b", "c" ];
    arr = flatten(arr);
    assert((arr == [ "a", "b", "c" ]), arr.to!string);
}