CompareInfo.LastIndexOf Method (String, Char, Int32, CompareOptions)
Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.
Assembly: mscorlib (in mscorlib.dll)
abstract LastIndexOf : source:string * value:char * startIndex:int * options:CompareOptions -> int override LastIndexOf : source:string * value:char * startIndex:int * options:CompareOptions -> int
- Type: System.String
The string to search.
- Type: System.Char
The character to locate within source.
- Type: System.Int32
The zero-based starting index of the backward search.
Return ValueType: System.Int32
The zero-based index of the last occurrence of value, if found, within the section of source that extends from the beginning of source to startIndex, using the specified comparison options; otherwise, -1. Returns startIndex if value is an ignorable character.
The source string is searched backward starting at startIndex and ending at the beginning of the string.
The CompareOptions.StringSort value is not valid for this method.
If options does not include the Ordinal value, 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. If options includes the Ordinal value, this overload performs an ordinal (culture-insensitive) search. A character is considered equivalent to another character only if the Unicode values are the same. 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.
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 method always returns startIndex, which is the character position at which the search begins. In the following example, the method is used to find a soft hyphen (U+00AD) that precedes the final "m” in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, a culture-sensitive search returns the index position of the "m". An ordinal search, however, successfully finds the soft hyphen in one string and reports that it is absent from the second string.
The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string. Note that IndexOf and LastIndexOf are searching in different portions of the string, even with the same startIndex parameter.