Chars Property
Collapse the table of content
Expand the table of content

String.Chars Property (Int32)


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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public char this[
	int index
] { get; }


Type: System.Int32

A position in the current string.

Property Value

Type: System.Char

The object at position index.

Exception Condition

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 object 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 Unicode characters instead of Char objects. For more information, seethe "Char Objects and Unicode Characters" section in the String class overview.

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.

private void SetCurrentFolder()
    string folderName = "TestFolder";
    Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().CurrentFolder = inBox.
        MessageBox.Show("There is no folder named " + folderName +
            ".", "Find Folder Name");

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 ++)
               Console.WriteLine("{0} is a hexadecimal digit.", myString[i]); 
               Console.WriteLine("{0} is not a hexadecimal digit.", myString[i]); 
         // The example produces output like the following:
         //    Type a string : 3f5EaZ
         //    3 is a hexadecimal digit.
         //    f is a hexadecimal digit.
         //    5 is a hexadecimal digit.
         //    E is a hexadecimal digit.
         //    a is a hexadecimal digit.
         //    Z is not a hexadecimal digit.            

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft