lastIndexOf

Searches for the last occurrence of a substring 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)
  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)
    @safe pure
    ptrdiff_t
    lastIndexOf
    (
    Char1
    Char2
    )
    (
    const(Char1)[] s
    ,
    const(Char2)[] sub
    ,
    in size_t startIdx
    ,
    in CaseSensitive cs = Yes.caseSensitive
    )
    if (
    isSomeChar!Char1 &&
    )

Parameters

s const(Char1)[]

string to search for sub in

sub const(Char2)[]

substring 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

The index of the last 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 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.

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(lastIndexOf(s, "ll") == 2);
assert(lastIndexOf(s, "Zo") == -1);
assert(lastIndexOf(s, "lL", No.caseSensitive) == 2);
import std.typecons : No;

string s = "Hello World";
assert(lastIndexOf(s, "ll", 4) == 2);
assert(lastIndexOf(s, "Zo", 128) == -1);
assert(lastIndexOf(s, "lL", 3, No.caseSensitive) == -1);

Meta