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.
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#] 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++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
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
String Class | String Members | System Namespace | Char | GetEnumerator | Int32