Java.lang Package Classes

Java String - charAt() Method



The java.lang.String.charAt() method returns the character at specified index in the given string. An index argument must be in the ranges from 0 to length() - 1.

Syntax

public char charAt(int index)

Parameters

index specify the index of the char value.

Return Value

Returns the char value at the specified index of this string. The first char value is at index 0.

Exception

Throws IndexOutOfBoundsException, if the index argument is negative or not less than the length of this string.

Example:

In the example below, charAt() method returns the character at specified index in the string called MyString.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!";
    System.out.println(MyString.charAt(1));   
  }
}

The output of the above code will be:

e

❮ Java.lang - String