String charAt()

Java String charAt()

The Java String charAt(int index) method returns the character at the specified index in a string. The index value that we pass in this method should be between 0 and (length-1). 

NOTE: String index starts with 0. The index of first character is 0 in string. For example: s.charAt(0) would return the first character of the string.


Here is the syntax of this method −

public char charAt(int index)


index- It should be integer type and it should be between 0 to length-1.


charAt() returns character at the specified position. If given index is not in between o and length-1 then it will throw an StringIndexOutOfBoundsException.


StringIndexOutOfBoundsException- If index is negative or greater then the length of the String.