0 out of 1 rated this helpful - Rate this topic

String.LastIndexOf Method

Reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method LastIndexOf(Char) Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance.
Public method LastIndexOf(String) Reports the zero-based index position of the last occurrence of a specified string within this instance.
Public method LastIndexOf(Char, Int32) Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position.
Public method LastIndexOf(String, Int32) Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position.
Public method LastIndexOf(String, StringComparison) Reports the zero-based index of the last occurrence of a specified string within the current String object. A parameter specifies the type of search to use for the specified string.
Public method LastIndexOf(Char, Int32, Int32) Reports the zero-based index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at a specified character position and examines a specified number of character positions.
Public method LastIndexOf(String, Int32, Int32) Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and examines a specified number of character positions.
Public method LastIndexOf(String, Int32, StringComparison) Reports the zero-based index of the last occurrence of a specified string within the current String object. Parameters specify the starting search position in the current string, and type of search to use for the specified string.
Public method LastIndexOf(String, Int32, Int32, StringComparison) Reports the zero-based index position of the last occurrence of a specified string within this instance. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string.
Top
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Not right

Given description is actually incorrect. Guess what this code will return ? var i = "0123.567(9.".LastIndexOf('.', 9);  It seems that result should be 10, but it's 4.

This Behavior Is Expected

This is the documented behavior. In the method call "0123.567(9.".LastIndexOf('.', 9);, you're beginning the comparison at the tenth character position (the character at index 9). So the substring in which you're searching for a match is 0123.567(9. Since you've omitted the final period from the comparison, the method correctly identifies the period at index 4 as being the last period in the substring searched.

The important thing to remember about the String.LastIndexOf overloads that include a parameter that specifies the starting position of the search is that the search begins from that character position to the beginning of the string. According to the documentation for String.LastIndexOf(String, Int32), "The search begins at the startIndex character position of this instance and proceeds backward toward the beginning until either value is found or the first character position has been examined."

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation