How to: Access Characters in Strings in Visual Basic

This example demonstrates how to use the Chars[] property to access the character at the specified location in a string.


Sometimes it is useful to have data about the characters in your string and the positions of those characters within your string. You can think of a string as an array of characters (Char instances); you can retrieve a particular character by referencing the index of that character through the Chars[] property.

    Dim myString As String = "ABCDE"
    Dim myChar As Char

    ' Assign "D" to myChar.
    myChar = myString.Chars(3)

The index parameter of the Chars[] property is zero-based.

Robust Programming

The Chars[] property returns the character at the specified position. However, some Unicode characters can be represented by more than one character. For more information on how to work with Unicode characters, see How to: Convert a String to an Array of Characters.

The Chars[] property throws an IndexOutOfRangeException exception if the index parameter is greater than or equal to the length of the string, or if it is less than zero

See also