lastIndexOf

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

  1. ptrdiff_t lastIndexOf(const(Char)[] s, dchar c, CaseSensitive cs)
  2. ptrdiff_t lastIndexOf(const(Char)[] s, dchar c, size_t startIdx, CaseSensitive cs)
    @safe pure
    ptrdiff_t
    lastIndexOf
    (
    Char
    )
    (
    const(Char)[] s
    ,
    in dchar c
    ,
    in size_t startIdx
    ,
    in CaseSensitive cs = Yes.caseSensitive
    )
    if ()
  3. ptrdiff_t lastIndexOf(const(Char1)[] s, const(Char2)[] sub, CaseSensitive cs)
  4. ptrdiff_t lastIndexOf(const(Char1)[] s, const(Char2)[] sub, size_t startIdx, CaseSensitive cs)

Parameters

s const(Char)[]

string to search for c in

c dchar

character to search for in s

startIdx size_t

index of 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 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