CompareInfo.LastIndexOf Method (String, Char)


Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

abstract LastIndexOf : 
        source:string *
        value:char -> int
override LastIndexOf : 
        source:string *
        value:char -> int


Type: System.String

The string to search.

Type: System.Char

The character to locate within source.

Return Value

Type: System.Int32

The zero-based index of the last occurrence of value, if found, within source; otherwise, -1.

Exception Condition

source is null.

The source string is searched backward starting at the end of the string and ending at the beginning of the string.

This overload performs a culture-sensitive search. If the character is a Unicode value representing a precomposed character, such as the ligature "Æ" (U+00C6), it might be considered equivalent to any occurrence of its components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. To perform an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if the Unicode values are the same, you should call one of the overloads that has a parameter of type CompareOptions and use the Ordinal value. Overloads of String.LastIndexOf that search for a character perform an ordinal search, whereas those that search for a string perform a culture-sensitive search.


When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for security comparisons.

Notes to Callers:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive sort. In a culture-sensitive search, if value is an ignorable character, the result is equivalent to searching with that character removed. In this case, the LastIndexOf(String, Char) method always returns the last index position in source to indicate that the match is found at the end of source. In the following example, the LastIndexOf(String, Char) method is used to find the soft hyphen (U+00AD) in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, the method returns the last index position in the string to indicate that it has found a match at the end of the string.

No code example is currently available or this language may not be supported.

The following example determines the indexes of the first and last occurrences of a character or a substring within a string.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft