[Issue 20184] String maxsplit

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 1 20:16:52 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20184

Jon Degenhardt <jrdemail2000-dlang at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jrdemail2000-dlang at yahoo.co
                   |                            |m

--- Comment #1 from Jon Degenhardt <jrdemail2000-dlang at yahoo.com> ---
This can be achieved using 'splitter' and 'take' or another range iteration
algorithm that limits the number of candidates selected.

e.g.

assert("a|bc|def".splitter('|').take(4).equal([ "a", "bc", "def" ]));
assert("a|bc|def".splitter('|').take(3).equal([ "a", "bc", "def" ]));
assert("a|bc|def".splitter('|').take(2).equal([ "a", "bc" ]));
assert("a|bc|def".splitter('|').take(1).equal([ "a" ]));

'splitter' (from std.algorithm) is a lazy version of 'split', which is eager.
It produces an input range. 'take' (from std.range) takes the first N elements
from an input range. 'take' is also lazy. To convert it to a fully realized
array similar to the result of 'split' use 'array' (from std.array) or another
range "eager" range algorithm. e.g.

auto x = "a|bc|def".splitter('|').take(2).array;
assert(x.length == 2);
assert (x[0] == "a");
assert (x[1] == "bc");

--


More information about the Digitalmars-d-bugs mailing list