LastIndexOf Method (String)

String::LastIndexOf Method (String^)


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

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

int LastIndexOf(
	String^ value


Type: System::String^

The string to seek.

Return Value

Type: System::Int32

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.

Exception Condition

value is null.

Index numbering starts from zero. That is, the first character in the string is at index zero and the last is at 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 LastIndexOf(String^) method always returns String::Length – 1, which represents the last index position in the current instance. In the following example, the 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. If the example is run on the .NET Framework 4 or later, 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.

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

Notes to Callers:

As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To find the last index of a substring within a string instance by using the comparison rules of the current culture, call the LastIndexOf(String^, StringComparison) method overload with a value of StringComparison::CurrentCulture for its comparisonType parameter.

The following example removes opening and closing HTML tags from a string if the tags begin and end the string. If a string ends with a closing bracket character (">"), the example uses the LastIndexOf method to locate the start of the end tag.

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
© 2015 Microsoft