View source code
Display the source code in std/algorithm/searching.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.algorithm.searching.startsWith
Checks whether the given input range starts with (one of) the given needle(s).
Prototypes
uint startsWith(alias pred, Range, Needles...)( Range doesThisStart, Needles withOneOfThese ) if (isInputRange!Range && Needles.length > 1 && is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[0])) : bool) && is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[1..__dollar])) : uint)); bool startsWith(alias pred, R1, R2)( R1 doesThisStart, R2 withThis ) if (isInputRange!R1 && isInputRange!R2 && is(typeof(binaryFun!pred(doesThisStart.front, withThis.front)) : bool)); bool startsWith(alias pred, R, E)( R doesThisStart, E withThis ) if (isInputRange!R && is(typeof(binaryFun!pred(doesThisStart.front, withThis)) : bool));
Parameters
Name | Description |
---|---|
pred | Predicate to use in comparing the elements of the haystack and the needle(s). |
doesThisStart | The input range to check. |
withOneOfThese | The needles against which the range is to be checked, which may be individual elements or input ranges of elements. |
withThis | The single needle to check, which may be either a single element or an input range of elements. |
Returns
0 if the needle(s) do not occur at the beginning of the given range;
otherwise the position of the matching needle, that is, 1 if the range starts
with
, 2 if it starts with withOneOfThese
[0]
, and so
on.
withOneOfThese
[1]
In the case where
starts with multiple of the ranges or
elements in doesThisStart
, then the shortest one matches (if there are
two which match which are of the same length (e.g. withOneOfThese
"a"
and 'a'
), then
the left-most of them in the argument
list matches).
Example
assert(startsWith("abc", "")); assert(startsWith("abc", "a")); assert(!startsWith("abc", "b")); assert(startsWith("abc", 'a', "b") == 1); assert(startsWith("abc", "b", "a") == 2); assert(startsWith("abc", "a", "a") == 1); assert(startsWith("abc", "ab", "a") == 2); assert(startsWith("abc", "x", "a", "b") == 2); assert(startsWith("abc", "x", "aa", "ab") == 3); assert(startsWith("abc", "x", "aaa", "sab") == 0); assert(startsWith("abc", "x", "aaa", "a", "sab") == 3); alias C = Tuple!(int, "x", int, "y"); assert(startsWith!"a.x == b"([ C(1,1), C(1,2), C(2,2) ], [1, 1])); assert(startsWith!"a.x == b"([ C(1,1), C(2,1), C(2,2) ], [1, 1], [1, 2], [1, 3]) == 2);