CompareInfo.LastIndexOf Method (String, String)

Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string.

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

abstract LastIndexOf : 
        source:string * 
        value:string -> int  
override LastIndexOf : 
        source:string * 
        value:string -> int

Parameters

source
Type: System.String

The string to search.

value
Type: System.String

The string to locate within source.

Return Value

Type: System.Int32
The zero-based index of the last occurrence of value, if found, within source; otherwise, -1.

ExceptionCondition
ArgumentNullException

source is a null reference (Nothing in Visual Basic).

-or-

value is a null reference (Nothing in Visual Basic).

The source string is searched backward starting at the end of the string and ending at the beginning of the string.

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. To perform an ordinal (culture-insensitive) search, where the Unicode values are compared, you should call one of the overloads that has a parameter of type CompareOptions and use the CompareOptions.Ordinal value.

NoteNote

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.

Notes to Callers

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, 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, String) method always returns source.Length – 1, which represents the last index position in source. In the following example, the LastIndexOf(String, 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.

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

The following example determines the indexes of the first and last occurrences of a character or a substring within a string.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft