String charAt example in java

The charAt() method returns the char value at the specified index. An index ranges from 0 to length() – 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

package com.java.connect.string;
 
// String charAt example in java
public class StringCharAtExample {
    public static void main(String[] args) {
        //
        // declare the String with some text
        //
        String string = "some text here";
 
        //
        // To get the char at from given index.
        // You need to use String's charAt method as shown below
        //
 
        char chatAt = string.charAt(5);
 
        System.out.println("The char at 5th index in given string is : " + chatAt);
 
    }
}

The output of above example will be as:

The char at 5th index in given string is : t