CompareInfo::IndexOf Method (String^, Char)
Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- source
-
Type:
System::String^
The string to search.
- value
-
Type:
System::Char
The character to locate within source.
Return Value
Type: System::Int32The zero-based index of the first occurrence of value, if found, within source; otherwise, -1. Returns 0 (zero) if value is an ignorable character.
| Exception | Condition |
|---|---|
| ArgumentNullException | source is null. |
This method searches the source string from the beginning of the string to the end of the string.
This overload performs a culture-sensitive search. If value represents a precomposed Unicode 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 their Unicode code points are the same, you should call an overload that has a parameter of type CompareOptions and use the CompareOptions::Ordinal value. Unlike CompareInfo::IndexOf, which performs a culture-sensitive comparison, overloads of the String::IndexOf method that search for a character perform an ordinal comparison and overloads that search for a string perform a culture-sensitive comparison.
Note |
|---|
When possible, you should use 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) method always returns 0 (zero) to indicate that the match is found at the beginning of source. In the following example, the IndexOf(String^, Char) method is used to find the soft hyphen (U+00AD) 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 0 (zero) to indicate that it has found a match at the beginning of the string.
The following example determines the indexes of the first and last occurrences of a character or a substring within 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; // Searches for the ligature Æ. String^ myStr = "Is AE or ae the same as Æ or æ?"; Console::WriteLine(); Console::WriteLine( "No options : {0}", myStr ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE" ), myComp->LastIndexOf( myStr, "AE" ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae" ), myComp->LastIndexOf( myStr, "ae" ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ' ), myComp->LastIndexOf( myStr, L'Æ' ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ' ), myComp->LastIndexOf( myStr, L'æ' ) ); Console::WriteLine( "Ordinal : {0}", myStr ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", CompareOptions::Ordinal ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", CompareOptions::Ordinal ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::Ordinal ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}", myStr ); PrintMarker( " AE : ", myComp->IndexOf( myStr, "AE", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", CompareOptions::IgnoreCase ) ); PrintMarker( " ae : ", myComp->IndexOf( myStr, "ae", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", CompareOptions::IgnoreCase ) ); PrintMarker( " Æ : ", myComp->IndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ) ); PrintMarker( " æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', 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(); Console::WriteLine( "No options : {0}", myStr ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308" ), myComp->LastIndexOf( myStr, "U\u0308" ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308" ), myComp->LastIndexOf( myStr, "u\u0308" ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü' ), myComp->LastIndexOf( myStr, L'Ü' ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü' ), myComp->LastIndexOf( myStr, L'ü' ) ); Console::WriteLine( "Ordinal : {0}", myStr ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", CompareOptions::Ordinal ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", CompareOptions::Ordinal ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::Ordinal ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::Ordinal ) ); Console::WriteLine( "IgnoreCase : {0}", myStr ); PrintMarker( " U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", CompareOptions::IgnoreCase ) ); PrintMarker( " u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", CompareOptions::IgnoreCase ) ); PrintMarker( " Ü : ", myComp->IndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ) ); PrintMarker( " ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::IgnoreCase ) ); } /* This code produces the following output. No options : Is AE or ae the same as Æ or æ? AE : f l ae : f l Æ : f l æ : f l Ordinal : Is AE or ae the same as Æ or æ? AE : b ae : b Æ : b æ : b IgnoreCase : Is AE or ae the same as Æ or æ? AE : f l ae : f l Æ : f l æ : f l No options : Is U" or u" the same as Ü or ü? U" : f l u" : f l Ü : f l ü : f l Ordinal : Is U" or u" the same as Ü or ü? U" : b u" : b Ü : b ü : b IgnoreCase : Is U" or u" the same as Ü or ü? 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
