String::LastIndexOf Method (String)
Reports the zero-based index position of the last occurrence of a specified string within this instance.
Assembly: mscorlib (in mscorlib.dll)
- Type: System::String
The string to seek.
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 method always returns String::Length – 1, which represents the last index position in the current instance. In the following example, the 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.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.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8