countUntilLastOccurrenceOf

Finds the index of the last occurrence of a needle in a haystack. The needle may be a single element (character) or an array of the same type as the haystack.

Overload that takes a needle instead of a predicate.

pure
countUntilLastOccurrenceOf
(
Haystack
Needle
)
(
const scope Haystack haystack
,
const scope Needle needle
)

Parameters

haystack Haystack

The array to search.

needle Needle

The element or array to search for.

Return Value

Type: auto

The index of the last occurrence of needle in haystack, or -1 if none were found.

Examples

import std.conv : to;

{
    enum haystack = "haystack";
    enum needle = "a";
    enum index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == 5), index.to!string);
}
{
    enum haystack = "1234567890";
    enum needle = '7';
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == 6), index.to!string);
}
{
    enum haystack = "123456789";
    enum needle = '0';
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == -1), index.to!string);
}
{
    enum haystack = "1234567890";
    enum needle = "";
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    static assert((index == 0), index.to!string);
}
{
    enum haystack = "";
    enum needle = "";
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == 0), index.to!string);
}
{
    enum haystack = "";
    enum needle = "blarp";
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == -1), index.to!string);
}
{
    static immutable haystack = [ 1, 2, 3, 4, 5, 1, 2, 3 ];
    enum needle = 1;
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == 5), index.to!string);
}
{
    static immutable haystack = [ 1, 2, 3, 1, 2, 3, 4, 5, 6 ];
    static immutable needle = [ 1, 2, 3 ];
    immutable index = haystack.countUntilLastOccurrenceOf(needle);
    assert((index == 3), index.to!string);
}

See Also