Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Chars Property

Gets the character at a specified character position in this instance.

[C#] In C#, this property is the indexer for the String class.

[Visual Basic]
Public Default ReadOnly Property Chars( _
   ByVal index As Integer _
) As Char
[C#]
public char this[
 int index
] {get;}
[C++]
public: __property __wchar_t get_Chars(
 int index
);
[JScript]
returnValue = StringObject.Chars(index);
-or-
returnValue = StringObject(index);

[JScript] In JScript, you can use the default indexed properties defined by a type, but you cannot explicitly define your own. However, specifying the expando attribute on a class automatically provides a default indexed property whose type is Object and whose index type is String.

Arguments [JScript]

index
A character position in this instance.

Parameters [Visual Basic, C#, C++]

index
A character position in this instance.

Property Value

A Unicode character.

Exceptions

Exception Type Condition
IndexOutOfRangeException index is greater than or equal to the length of this object or less than zero.

Remarks

The index parameter is zero-based.

This property returns the Char at the position specified by the index parameter. However, a Unicode character might be represented by more than one Char. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

Example

[Visual Basic, C#, C++] The following code example demonstrates how you can use this indexer in a routine to validate a string.

[Visual Basic] 
Console.Write("Type a string : ")
Dim myString As String = Console.ReadLine()
Dim i As Integer
For i = 0 To myString.Length - 1
   If Uri.IsHexDigit(myString.Chars(i)) Then
      Console.WriteLine("{0} is a hexadecimal digit.", myString.Chars(i))
   Else
      Console.WriteLine("{0} is not a hexadecimal digit.", myString.Chars(i))
   End If 

[C#] 
Console.Write("Type a string : ");
string myString = Console.ReadLine();
for (int i = 0; i < myString.Length; i ++)
if(Uri.IsHexDigit(myString[i]))
       Console.WriteLine("{0} is a hexadecimal digit.", myString[i]); 
else
   Console.WriteLine("{0} is not a hexadecimal digit.", myString[i]); 

[C++] 
Console::Write(S"Type a string : ");
String* myString = Console::ReadLine();
for (int i = 0; i < myString->Length; i ++)
    if(Uri::IsHexDigit(myString->Chars[i]))
        Console::WriteLine(S"{0} is a hexadecimal digit.", __box(myString->Chars[i])); 
    else
        Console::WriteLine(S"{0} is not a hexadecimal digit.", __box(myString->Chars[i])); 

[JScript] No example is available for JScript. To view a Visual Basic, 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, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | Char | GetEnumerator | Int32

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