indexOf

Searches for a substring in a string or range.

Parameters

s Range

string or ForwardRange of characters to search for sub in

sub const(Char)[]

substring to search for in s

Return Value

Type: ptrdiff_t

The index of the first occurrence of sub in s. If sub is not found or startIdx is greater than or equal to s.length, then -1 is returned. If the arguments 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.

Bugs

Does not work with case-insensitive strings where the mapping of std.uni.toLower and std.uni.toUpper is not 1:1.

Examples

import std.typecons : No;

string s = "Hello World";
assert(indexOf(s, "Wo", 4) == 6);
assert(indexOf(s, "Zo", 100) == -1);
assert(indexOf(s, "wo", 3, No.caseSensitive) == 6);
import std.typecons : No;

string s = "Hello World";
assert(indexOf(s, "Wo") == 6);
assert(indexOf(s, "Zo") == -1);
assert(indexOf(s, "wO", No.caseSensitive) == 6);

Meta