Function std.string.indexOfAny
Searches the string haystack
for one of the characters in needles
starting at index startIdx
. If startIdx
is not given, it defaults to 0.
ptrdiff_t indexOfAny(Char, Char2)(
const(Char)[] haystack,
const(Char2)[] needles,
in CaseSensitive cs = Yes .caseSensitive
) pure @safe
if (isSomeChar!Char && isSomeChar!Char2);
ptrdiff_t indexOfAny(Char, Char2)(
const(Char)[] haystack,
const(Char2)[] needles,
in size_t startIdx,
in CaseSensitive cs = Yes .caseSensitive
) pure @safe
if (isSomeChar!Char && isSomeChar!Char2);
Parameters
Name | Description |
---|---|
haystack | string to search for needles in |
needles | characters to search for in haystack |
startIdx | index of a well-formed code point in haystack to start
searching from; defaults to 0 |
cs | specifies whether comparisons are case-sensitive
(Yes ) or not (No ) |
Returns
The index of the first occurrence of any of the elements of needles
in
haystack
. If no element of needles
is found or startIdx
is greater
than or equal to haystack
, then -1 is returned. If the
parameters are not valid UTF, the result will still be either -1 or in
the range [startIdx
.. haystack
], but will not be reliable
otherwise.
Throws
If the sequence starting at startIdx
does not represent a well-formed
code point, then a UTFException
may be thrown.
Example
import std .conv : to;
ptrdiff_t i = "helloWorld" .indexOfAny("Wr");
writeln(i); // 5
i = "öällo world" .indexOfAny("lo ");
writeln(i); // 4
Example
import std .conv : to;
ptrdiff_t i = "helloWorld" .indexOfAny("Wr", 4);
writeln(i); // 5
i = "Foo öällo world" .indexOfAny("lh", 3);
writeln(i); // 8
Authors
Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis