CompareInfo::LastIndexOf Method (String^, String^, Int32)
Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- source
-
Type:
System::String^
The string to search.
- value
-
Type:
System::String^
The string to locate within source.
- startIndex
-
Type:
System::Int32
The zero-based starting index of the backward search.
Return Value
Type: System::Int32The zero-based index of the last occurrence of value, if found, within the section of source that extends from the beginning of source to startIndex; otherwise, -1. Returns startIndex if value is an ignorable character.
| Exception | Condition |
|---|---|
| ArgumentNullException | source is null. -or- value is null. |
| ArgumentOutOfRangeException | startIndex is outside the range of valid indexes for source. |
The source string is searched backward starting at startIndex 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.
Note |
|---|
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^, Int32) method always returns startIndex, which is the character position at which the search begins. In the following example, the LastIndexOf(String^, String^, Int32) method is used to find a substring that includes a soft hyphen (U+00AD) and that precedes or includes the final "m" in a string. Because the soft hyphen in the search string is ignored, calling the method to find a substring that consists of the soft hyphen and "m" returns the position of the "m" in the string, whereas calling it to find a substring that consists of the soft hyphen and "n" returns the position of the "n". When the search string contains only the soft hyphen, the method returns the index of the "m", which represents the value of startIndex.
The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string. Note that IndexOf and LastIndexOf are searching in different portions of the string, even with the same startIndex parameter.
using namespace System; using namespace System::Globalization; void PrintMarker( String^ Prefix, int First, int Last ) { // Determines the size of the array to create. int mySize; if ( Last > First ) mySize = Last; else mySize = First; if ( mySize > -1 ) { // Creates an array of Char to hold the markers. array<Char>^myCharArr = gcnew array<Char>(mySize + 1); // Inserts the appropriate markers. if ( First > -1 ) myCharArr[ First ] = 'f'; if ( Last > -1 ) myCharArr[ Last ] = 'l'; if ( First == Last ) myCharArr[ First ] = 'b'; // Displays the array of Char as a String. Console::WriteLine( "{0}{1}", Prefix, gcnew String( myCharArr ) ); } else Console::WriteLine( Prefix ); } int main() { // Creates CompareInfo for the InvariantCulture. CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo; // iS is the starting index of the substring. int iS = 20; // myT1 is the string used for padding. String^ myT1; // Searches for the ligature Æ. String^ myStr = "Is AE or ae the same as Æ or æ?"; Console::WriteLine(); myT1 = gcnew String( '-',iS ); Console::WriteLine( "IndexOf( String, *, {0}, * )", iS ); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS ), -1 ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS ), -1 ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS ), -1 ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS ), -1 ); Console::WriteLine( "Ordinal : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::Ordinal ), -1 ); Console::WriteLine( "IgnoreCase : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ), -1 ); myT1 = gcnew String( '-',myStr->Length - iS - 1 ); Console::WriteLine( "LastIndexOf( String, *, {0}, * )", iS ); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " AE : ", -1, myComp->LastIndexOf( myStr, "AE", iS ) ); PrintMarker( " ae : ", -1, myComp->LastIndexOf( myStr, "ae", iS ) ); PrintMarker( " Æ : ", -1, myComp->LastIndexOf( myStr, L'Æ', iS ) ); PrintMarker( " æ : ", -1, myComp->LastIndexOf( myStr, L'æ', iS ) ); Console::WriteLine( "Ordinal : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " AE : ", -1, myComp->LastIndexOf( myStr, "AE", iS, CompareOptions::Ordinal ) ); PrintMarker( " ae : ", -1, myComp->LastIndexOf( myStr, "ae", iS, CompareOptions::Ordinal ) ); PrintMarker( " Æ : ", -1, myComp->LastIndexOf( myStr, L'Æ', iS, CompareOptions::Ordinal ) ); PrintMarker( " æ : ", -1, myComp->LastIndexOf( myStr, L'æ', iS, CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " AE : ", -1, myComp->LastIndexOf( myStr, "AE", iS, CompareOptions::IgnoreCase ) ); PrintMarker( " ae : ", -1, myComp->LastIndexOf( myStr, "ae", iS, CompareOptions::IgnoreCase ) ); PrintMarker( " Æ : ", -1, myComp->LastIndexOf( myStr, L'Æ', iS, CompareOptions::IgnoreCase ) ); PrintMarker( " æ : ", -1, myComp->LastIndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ) ); // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis. myStr = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?"; Console::WriteLine(); myT1 = gcnew String( '-',iS ); Console::WriteLine( "IndexOf( String, *, {0}, * )", iS ); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS ), -1 ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS ), -1 ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS ), -1 ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS ), -1 ); Console::WriteLine( "Ordinal : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS, CompareOptions::Ordinal ), -1 ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::Ordinal ), -1 ); Console::WriteLine( "IgnoreCase : {0}{1}", myT1, myStr->Substring( iS ) ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS, CompareOptions::IgnoreCase ), -1 ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ), -1 ); myT1 = gcnew String( '-',myStr->Length - iS - 1 ); Console::WriteLine( "LastIndexOf( String, *, {0}, * )", iS ); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " U\u0308 : ", -1, myComp->LastIndexOf( myStr, "U\u0308", iS ) ); PrintMarker( " u\u0308 : ", -1, myComp->LastIndexOf( myStr, "u\u0308", iS ) ); PrintMarker( " Ü : ", -1, myComp->LastIndexOf( myStr, L'Ü', iS ) ); PrintMarker( " ü : ", -1, myComp->LastIndexOf( myStr, L'ü', iS ) ); Console::WriteLine( "Ordinal : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " U\u0308 : ", -1, myComp->LastIndexOf( myStr, "U\u0308", iS, CompareOptions::Ordinal ) ); PrintMarker( " u\u0308 : ", -1, myComp->LastIndexOf( myStr, "u\u0308", iS, CompareOptions::Ordinal ) ); PrintMarker( " Ü : ", -1, myComp->LastIndexOf( myStr, L'Ü', iS, CompareOptions::Ordinal ) ); PrintMarker( " ü : ", -1, myComp->LastIndexOf( myStr, L'ü', iS, CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 ); PrintMarker( " U\u0308 : ", -1, myComp->LastIndexOf( myStr, "U\u0308", iS, CompareOptions::IgnoreCase ) ); PrintMarker( " u\u0308 : ", -1, myComp->LastIndexOf( myStr, "u\u0308", iS, CompareOptions::IgnoreCase ) ); PrintMarker( " Ü : ", -1, myComp->LastIndexOf( myStr, L'Ü', iS, CompareOptions::IgnoreCase ) ); PrintMarker( " ü : ", -1, myComp->LastIndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ) ); } /* This code produces the following output. IndexOf( String, *, 20, * ) Original : Is AE or ae the same as Æ or æ? No options : -------------------- as Æ or æ? AE : f ae : f Æ : f æ : f Ordinal : -------------------- as Æ or æ? AE : ae : Æ : f æ : f IgnoreCase : -------------------- as Æ or æ? AE : f ae : f Æ : f æ : f LastIndexOf( String, *, 20, * ) Original : Is AE or ae the same as Æ or æ? No options : Is AE or ae the same ---------- AE : l ae : l Æ : l æ : l Ordinal : Is AE or ae the same ---------- AE : l ae : l Æ : æ : IgnoreCase : Is AE or ae the same ---------- AE : l ae : l Æ : l æ : l IndexOf( String, *, 20, * ) Original : Is U" or u" the same as Ü or ü? No options : -------------------- as Ü or ü? U" : f u" : f Ü : f ü : f Ordinal : -------------------- as Ü or ü? U" : u" : Ü : f ü : f IgnoreCase : -------------------- as Ü or ü? U" : f u" : f Ü : f ü : f LastIndexOf( String, *, 20, * ) Original : Is U" or u" the same as Ü or ü? No options : Is U" or u" the same ---------- U" : l u" : l Ü : l ü : l Ordinal : Is U" or u" the same ---------- U" : l u" : l Ü : ü : IgnoreCase : Is U" or u" the same ---------- U" : l u" : l Ü : l ü : l */
Available since 1.1
