This documentation is archived and is not being maintained.

java.lang.String.charAt(int index)

Visual Studio 2005

Gets the character at the given index within the String.


public char charAt(int index)

Parameters

Parameter Description

index

The position within the String whose character is to be retrieved. A StringIndexOutofBoundsException will be thrown if the index is invalid.

The char at the given index within the String.

// string_charat.jsl

public class Program
{
    public static void main(String[] args)
    {
        String str = "Hello, World!";

        // Get the character at positions 0 and 12.
        int index1 = str.charAt(0);
        int index2 = str.charAt(12);

        // Print out the results.
        System.out.println("The character at position 0 is " +
            (char)index1);
        System.out.println("The character at position 12 is " +
            (char)index2);
    }
}

/*
Output:
The character at position 0 is H
The character at position 12 is !
*/
Show: