System.String.LastIndexOf Method

Reports the zero-based index position of the last occurrence of a specified string within this instance.


public int LastIndexOf (string value)


The string to seek.


The zero-based starting index position of value if that string is found, or -1 if it is not. If value is string.Empty, the return value is the last index position in this instance.


ArgumentNullException value is a null reference.


Index numbering starts from zero. That is, the first character in the string is at index zero and the last is at string.Length - 1.

The search begins at the last character position of this instance and proceeds backward toward the beginning until either value is found or the first character position has been examined.

This method performs a word (case-sensitive and culture-sensitive) search using the current culture.

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, 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 string.LastIndexOf(string) method always returns string.Length – 1, which represents the last index position in the current instance. In the following example, the string.LastIndexOf(string) 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. In each case, because the soft hyphen is an ignorable character, the result is the same as if the soft hyphen had not been included in value. When searching for a soft hyphen only, the method returns 6 and 5. These values correspond to the index of the last character in the two strings.

code reference: System.String.LastIndexOf#21


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,