Java String charAt() method with example
The Java String charAt() method returns the character at the specified index. The index value should lie between 0 and length()-1.
public char charAt(int index)
index- Index of the character to be returned.
returns character at the specified position.
StringIndexOutOfBoundsException- If index is negative or greater then the length of the String.
Example:To show working of charAt() method
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(String.java:658) at Gfg.main(File.java:9)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.