lastIndexOf

Searches for the last occurrence of a character in a string.

Parameters

s const(Char)[]

string to search for c in

c dchar

character to search for in s

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 last 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 ending 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(lastIndexOf(s, 'l') == 9);
assert(lastIndexOf(s, 'Z') == -1);
assert(lastIndexOf(s, 'L', No.caseSensitive) == 9);
import std.typecons : No;

string s = "Hello World";
assert(lastIndexOf(s, 'l', 4) == 3);
assert(lastIndexOf(s, 'Z', 1337) == -1);
assert(lastIndexOf(s, 'L', 7, No.caseSensitive) == 3);

Meta