Windows apps
Collapse the table of content
Expand the table of content
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
public char this[
 int index
] {get;}
public: __property __wchar_t get_Chars(
 int index
returnValue = StringObject.Chars(index);
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]

A character position in this instance.

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

A character position in this instance.

Property Value

A Unicode character.


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


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.


[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))
      Console.WriteLine("{0} is not a hexadecimal digit.", myString.Chars(i))
   End If 

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

Console::Write(S"Type a string : ");
String* myString = Console::ReadLine();
for (int i = 0; i < myString->Length; i ++)
        Console::WriteLine(S"{0} is a hexadecimal digit.", __box(myString->Chars[i])); 
        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.


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

© 2017 Microsoft