Function std.algorithm.mutation.strip
The strip
group of functions allow stripping of either leading, trailing,
or both leading and trailing elements.
The
function will stripLeft
strip
the front
of the range
,
the
function will stripRight
strip
the back
of the range
,
while the
function will strip
strip
both the front
and back
of the range
.
Note that the
and strip
functions require the stripRight
range
to
be a BidirectionalRange range
.
All of these functions come in two varieties: one takes a target element
,
where the range
will be stripped as long as this element
can be found.
The other takes a lambda predicate, where the range
will be stripped as
long as the predicate returns true.
Prototypes
Range strip(Range, E)( Range range, E element ) if (isBidirectionalRange!Range && is(typeof(range.front == element) : bool)); Range strip(alias pred, Range)( Range range ) if (isBidirectionalRange!Range && is(typeof(pred(range.back)) : bool));
Example
Strip leading and trailing elements equal to the target element
.
assert(" foobar ".strip(' ') == "foobar"); assert("00223.444500".strip('0') == "223.4445"); assert("ëëêéüŗōpéêëë".strip('ë') == "êéüŗōpéê"); assert([1, 1, 0, 1, 1].strip(1) == [0]); assert([0.0, 0.01, 0.01, 0.0].strip(0).length == 2);
Example
Strip leading and trailing elements while the predicate returns true.
assert(" foobar ".strip!(a => a == ' ')() == "foobar"); assert("00223.444500".strip!(a => a == '0')() == "223.4445"); assert("ëëêéüŗōpéêëë".strip!(a => a == 'ë')() == "êéüŗōpéê"); assert([1, 1, 0, 1, 1].strip!(a => a == 1)() == [0]); assert([0.0, 0.01, 0.5, 0.6, 0.01, 0.0].strip!(a => a < 0.4)().length == 2);
Example
Strip leading elements equal to the target element
.
assert(" foobar ".stripLeft(' ') == "foobar "); assert("00223.444500".stripLeft('0') == "223.444500"); assert("ůůűniçodêéé".stripLeft('ů') == "űniçodêéé"); assert([1, 1, 0, 1, 1].stripLeft(1) == [0, 1, 1]); assert([0.0, 0.01, 0.01, 0.0].stripLeft(0).length == 3);
Example
Strip leading elements while the predicate returns true.
assert(" foobar ".stripLeft!(a => a == ' ')() == "foobar "); assert("00223.444500".stripLeft!(a => a == '0')() == "223.444500"); assert("ůůűniçodêéé".stripLeft!(a => a == 'ů')() == "űniçodêéé"); assert([1, 1, 0, 1, 1].stripLeft!(a => a == 1)() == [0, 1, 1]); assert([0.0, 0.01, 0.10, 0.5, 0.6].stripLeft!(a => a < 0.4)().length == 2);
Example
Strip trailing elements equal to the target element
.
assert(" foobar ".stripRight(' ') == " foobar"); assert("00223.444500".stripRight('0') == "00223.4445"); assert("ùniçodêéé".stripRight('é') == "ùniçodê"); assert([1, 1, 0, 1, 1].stripRight(1) == [1, 1, 0]); assert([0.0, 0.01, 0.01, 0.0].stripRight(0).length == 3);
Example
Strip trailing elements while the predicate returns true.
assert(" foobar ".stripRight!(a => a == ' ')() == " foobar"); assert("00223.444500".stripRight!(a => a == '0')() == "00223.4445"); assert("ùniçodêéé".stripRight!(a => a == 'é')() == "ùniçodê"); assert([1, 1, 0, 1, 1].stripRight!(a => a == 1)() == [1, 1, 0]); assert([0.0, 0.01, 0.10, 0.5, 0.6].stripRight!(a => a > 0.4)().length == 3);