string class : LastIndexOf(string) method

Description

Gets the index of the last occurrence of the specified string in the caller string.

The search starts at the end of the string.

Syntax

stringInstance.LastIndexOf(string searchString)

Arguments

ClassNameDescription
stringsearchStringA search string.

Return value

ClassDescription
intIndex of the found position.

Sample code

1:

string str = "abcba";

2:

int index = str.LastIndexOf("b"); // The result (index) is 3.

Notes

It's a wrapper of the System.String.LastIndexOf(String).

However, this method has supported to the surrogate characters of UTF-8. In other words, It returns an index as number of characters. Please be aware it is not number of bytes.

The index is zero-based.

Returns -1 if it is not found.

Links for reference

Copyright © Rice All rights reserved.