Stuck with std.algorithm.splitter

bearophile bearophileHUGS at lycos.com
Sun Apr 3 17:18:40 PDT 2011


Aleksandar R.:

> I want to split a string into an array of strings. As much as it seems
> trivial I just can't make it work.
> 
> This is code I use for splitting:
> 
> -------------------------------------------------
> auto input = "foo|bar";             // sample input string
> auto parts = splitter(input, '|');  // split by pipe character
> -------------------------------------------------

import std.stdio, std.array;

void main() {
    string input = "foo|bar";
    //auto parts = split(input, '|'); // nope
    string[] parts = split(input, "|");

    if (parts.length > 1)
        writefln("%s, %s", parts[0], parts[1]);
    else
        writefln("%s", parts[0]);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list