String charAt() Method

String charAt() Method in Java with Example

Why use string “charAt” Method?

The charat method returns the character at the definite index. In this method index value should be between 0 and string length minus 1

Method Syntax:

Parameter input:

index – This Java method accepts only single input which is an int data type.

Method Returns:

This method returns a character type data based on the index input



Throws java.lang.StringIndexOutOfBoundsException if index value is not between 0 and String length minus one

Example 1:


Character at 0 position is: T
Character at 5th position is: i
Character at 22nd position is: M

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Some important things about this Java charAt method: