CompareInfo::IndexOf Method (String^, Char, Int32, Int32)
Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- source
-
Type:
System::String^
The string to search.
- value
-
Type:
System::Char
The character to locate within source.
- startIndex
-
Type:
System::Int32
The zero-based starting index of the search.
- count
-
Type:
System::Int32
The number of elements in the section to search.
Return Value
Type: System::Int32The zero-based index of the first occurrence of value, if found, within the section of source that starts at startIndex and contains the number of elements specified by count; otherwise, -1. Returns startIndex if value is an ignorable character.
| Exception | Condition |
|---|---|
| ArgumentNullException | source is null. |
| ArgumentOutOfRangeException | startIndex is outside the range of valid indexes for source. -or- count is less than zero. -or- startIndex and count do not specify a valid section in source. |
The source string is searched forward starting at startIndex and ending at startIndex + count - 1.
This overload performs a culture-sensitive search. If the character is a Unicode value representing a precomposed character, such as the ligature "Æ" (U+00C6), it might be considered equivalent to any occurrence of its components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. To perform an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if the Unicode values are the same, you should call one of the overloads that has a parameter of type CompareOptions and use the Ordinal value. Overloads of String::IndexOf that search for a character perform an ordinal search, whereas those that search for a string perform a culture-sensitive search.
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 is an ignorable character, the result is equivalent to searching with that character removed. In this case, the IndexOf(String^, Char, Int32, Int32) method always returns startIndex, which is the character position at which the search first began. In the following example, the IndexOf(String^, Char, Int32, Int32) method is used to find a soft hyphen (U+00AD) after an "n” in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, the method returns 1 to indicate that it has found a match at the position of the "n".
The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string.
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 = 8; // iL is the length of the substring. int iL = 18; // myT1 and myT2 are the strings used for padding. String^ myT1 = gcnew String( '-',iS ); String^ myT2; // Searches for the ligature Æ. String^ myStr = "Is AE or ae the same as Æ or æ?"; myT2 = gcnew String( '-',myStr->Length - iS - iL ); Console::WriteLine(); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS, iL ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS, iL ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS, iL ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL ) ); Console::WriteLine( "Ordinal : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, 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?"; myT2 = gcnew String( '-',myStr->Length - iS - iL ); Console::WriteLine(); Console::WriteLine( "Original : {0}", myStr ); Console::WriteLine( "No options : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS, iL ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL ) ); Console::WriteLine( "Ordinal : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::Ordinal ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) ); } /* This code produces the following output. Original : Is AE or ae the same as Æ or æ? No options : -------- ae the same as Æ ----- AE : b ae : b Æ : b æ : b Ordinal : -------- ae the same as Æ ----- AE : ae : b Æ : b æ : IgnoreCase : -------- ae the same as Æ ----- AE : f l ae : f l Æ : f l æ : f l Original : Is U" or u" the same as Ü or ü? No options : -------- u" the same as Ü ----- U" : b u" : b Ü : b ü : b Ordinal : -------- u" the same as Ü ----- U" : u" : b Ü : b ü : IgnoreCase : -------- u" the same as Ü ----- U" : f l u" : f l Ü : f l ü : f l */
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
