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.
1 out of 4 rated this helpful - Rate this topic

String.Chars Property

Updated: August 2010

Gets the character at a specified character position in the current String object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public char this[
	int index
] { get; }

Parameters

index
Type: System.Int32

A character position in the current String object.

Property Value

Type: System.Char
A Unicode character.
ExceptionCondition
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.

In C#, the Chars property is an indexer. In Visual Basic, it is the default property of the String class. Each Char object in the string can be accessed by using code such as the following.

string str1 = "Test";
for (int ctr = 0; ctr <= str1.Length - 1; ctr++ )
   Console.Write("{0} ", str1[ctr]);
// The example displays the following output: 
//      T e s t         

The following example demonstrates how you can use this indexer in a routine to validate a string.

			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]); 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

August 2010

Expanded the Remarks section.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.