This documentation is archived and is not being maintained.

String.IndexOf Method (Char)

.NET Framework 1.1

Reports the index of the first occurrence of the specified Unicode character in this instance.

[Visual Basic]
Overloads Public Function IndexOf( _
   ByVal value As Char _
) As Integer
[C#]
public int IndexOf(
 char value
);
[C++]
public: int IndexOf(
 __wchar_t value
);
[JScript]
public function IndexOf(
   value : Char
) : int;

Parameters

value
A Unicode character to seek.

Return Value

The index position of value if that character is found, or -1 if it is not.

Remarks

Index numbering starts from zero.

The search for value is case-sensitive.

This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar 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.

Example

[C#, C++] The following code example demonstrates how you can search a String for a character using the IndexOf method.

[C#] 
// Create a Unicode String with 5 Greek Alpha characters
String szGreekAlpha = new String('\u0319',5);
// Create a Unicode String with a Greek Omega character
String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1);

String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());

// Examine the result
Console.WriteLine(szGreekLetters);

// The first index of Alpha
int ialpha = szGreekLetters.IndexOf('\u0319');
// The last index of Omega
int iomega = szGreekLetters.LastIndexOf('\u03A9');

Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
    " and Omega last appears at index " + iomega + " in this String.");

[C++] 
// Create a Unicode String with 5 Greek Alpha characters
String* szGreekAlpha = new String(L'\x0319',5);
// Create a Unicode String with a Greek Omega character
wchar_t charArray5 [3] = {L'\x03A9',L'\x03A9',L'\x03A9'};
String* szGreekOmega = new String( charArray5 ,2,1);

String* szGreekLetters = String::Concat(szGreekOmega, szGreekAlpha, szGreekOmega->Clone());

// Examine the result
Console::WriteLine(szGreekLetters);

// The first index of Alpha
int ialpha = szGreekLetters->IndexOf(L'\x0319');
// The last index of Omega
int iomega = szGreekLetters->LastIndexOf(L'\x03A9');

Console::WriteLine(String::Concat("The Greek letter Alpha first appears at index ",Convert::ToString(ialpha) ));
Console::WriteLine(String::Concat(" and Omega last appears at index ", Convert::ToString(iomega), " in this String."));

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.IndexOf Overload List | Char | IndexOfAny | LastIndexOf | LastIndexOfAny

Show: