indexOf

Searches for a character in a string or range.

Parameters

s const(C)[]

string or InputRange of characters to search for c in

c dchar

character to search for in s

startIdx size_t

index to a well-formed code point in s to start searching from; defaults to 0

cs CaseSensitive

specifies whether comparisons are case-sensitive (Yes.caseSensitive) or not (No.caseSensitive).

Return Value

Type: ptrdiff_t

If c is found in s, then the index of its first occurrence is returned. If c is not found or startIdx is greater than or equal to s.length, then -1 is returned. If the parameters are not valid UTF, the result will still be either -1 or in the range [startIdx .. s.length], but will not be reliable otherwise.

Throws

If the sequence starting at startIdx does not represent a well-formed code point, then a std.utf.UTFException may be thrown.

Examples

import std.typecons : No;

string s = "Hello World";
assert(indexOf(s, 'W') == 6);
assert(indexOf(s, 'Z') == -1);
assert(indexOf(s, 'w', No.caseSensitive) == 6);
import std.typecons : No;

string s = "Hello World";
assert(indexOf(s, 'W', 4) == 6);
assert(indexOf(s, 'Z', 100) == -1);
assert(indexOf(s, 'w', 3, No.caseSensitive) == 6);

See Also

Meta