std.string.indexOf
- multiple declarations
Function indexOf
Searches for character in range.
ptrdiff_t indexOf(Range)
(
Range s,
dchar c,
CaseSensitive cs = Yes .caseSensitive
)
if (isInputRange!Range && isSomeChar!(ElementType!Range) && !isSomeString!Range);
ptrdiff_t indexOf(C)
(
C[] s,
dchar c,
CaseSensitive cs = Yes .caseSensitive
)
if (isSomeChar!C);
ptrdiff_t indexOf(Range)
(
Range s,
dchar c,
size_t startIdx,
CaseSensitive cs = Yes .caseSensitive
)
if (isInputRange!Range && isSomeChar!(ElementType!Range) && !isSomeString!Range);
ptrdiff_t indexOf(C)
(
C[] s,
dchar c,
size_t startIdx,
CaseSensitive cs = Yes .caseSensitive
)
if (isSomeChar!C);
Parameters
Name | Description |
---|---|
s | string or InputRange of characters to search in correct UTF format |
c | character to search for |
startIdx | starting index to a well-formed code point |
cs | Yes or No |
Returns
the index of the first occurrence of c
in s
with
respect to the start index startIdx
. If c
is not found, then -1
is returned.
If c
is found the value of the returned index is at least
startIdx
.
If the parameters are not valid UTF, the result will still
be in the range [-1 .. s.length], but will not be reliable otherwise.
Throws
If the sequence starting at startIdx
does not represent a well
formed codepoint, then a UTFException
may be thrown.
See Also
Example
import std .typecons : No;
string s = "Hello World";
writeln(indexOf(s, 'W')); // 6
writeln(indexOf(s, 'Z')); // -1
writeln(indexOf(s, 'w', No .caseSensitive)); // 6
Example
import std .typecons : No;
string s = "Hello World";
writeln(indexOf(s, 'W', 4)); // 6
writeln(indexOf(s, 'Z', 100)); // -1
writeln(indexOf(s, 'w', 3, No .caseSensitive)); // 6
Function indexOf
ptrdiff_t indexOf(Range, Char)
(
Range s,
const(Char)[] sub,
in CaseSensitive cs = Yes .caseSensitive
)
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) && isSomeChar!Char);
ptrdiff_t indexOf(Char1, Char2)
(
const(Char1)[] s,
const(Char2)[] sub,
in size_t startIdx,
in CaseSensitive cs = Yes .caseSensitive
) @safe
if (isSomeChar!Char1 && isSomeChar!Char2);
Parameters
Name | Description |
---|---|
s | string or ForwardRange of characters to search in correct UTF format |
sub | substring to search for |
startIdx | the index into s to start searching from |
cs | Yes or No |
Returns
the index of the first occurrence of sub
in s
with
respect to the start index startIdx
. If sub
is not found,
then -1
is returned.
If the arguments are not valid UTF, the result will still
be in the range [-1 .. s.length], but will not be reliable otherwise.
If sub
is found the value of the returned index is at least
startIdx
.
Throws
If the sequence starting at startIdx
does not represent a well
formed codepoint, then a UTFException
may be thrown.
Bugs
Does not work with case insensitive strings where the mapping of tolower and toupper is not 1:1.
Example
import std .typecons : No;
string s = "Hello World";
writeln(indexOf(s, "Wo", 4)); // 6
writeln(indexOf(s, "Zo", 100)); // -1
writeln(indexOf(s, "wo", 3, No .caseSensitive)); // 6
Example
import std .typecons : No;
string s = "Hello World";
writeln(indexOf(s, "Wo")); // 6
writeln(indexOf(s, "Zo")); // -1
writeln(indexOf(s, "wO", No .caseSensitive)); // 6
Authors
Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis