CompareInfo.LastIndexOf Method (String, String, CompareOptions)
Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.
Assembly: mscorlib (in mscorlib.dll)
abstract LastIndexOf : source:string * value:string * options:CompareOptions -> int override LastIndexOf : source:string * value:string * options:CompareOptions -> int
- Type: System.String
The string to search.
- Type: System.String
The string to locate within source.
Return ValueType: System.Int32
The zero-based index of the last occurrence of value, if found, within source, using the specified comparison options; otherwise, -1.
The source string is searched backward starting at the end of the string 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. A Unicode value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's 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, where the Unicode values are compared.
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 (that is, if options is not CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase), if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the CompareInfo.LastIndexOf method always returns source.Length – 1, which represents the last index position in source. In the following example, the CompareInfo.LastIndexOf method is used to find three substrings (a soft hyphen (U+00AD), a soft hyphen followed by "n", and a soft hyphen followed by "m") in two strings. Only one of the strings contains a soft hyphen. Because the soft hyphen is an ignorable character, a culture-sensitive search returns the same value that it would return if the soft hyphen were not included in the search string. An ordinal search, however, successfully finds the soft hyphen in one string and reports that it is absent from the second string.