Export (0) Print
Expand All

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:
property wchar_t Chars[int index] {
	wchar_t get (int index);
}

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.

No code example is currently available or this language may not be supported.

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.

Community Additions

ADD
Show:
© 2014 Microsoft