Java String.charAt() Method

 Posted On  | Yashwant Chavan 

In this tutorial you will learn about String.charAt() method. This method returns the char value at the specified index.

Refer below steps

  • String.charAt(int index) method returns the char value at the specified index. An index ranges start from 0 to length()-1.
  • It throws IndexOutOfBoundsException if the index argument is negative more than length of the string. Refer last scenario where we are trying to get char at 25th position.

Java String length example


public class StringCharAt {
    public static void main(String[] args) {
        String str = "technicalkeeda";

        char t = str.charAt(0);
        System.out.println("char at 0th index:- " + t);

        char n = str.charAt(4);
        System.out.println("char at 4th index:- " + n);

        char outOfIndex = str.charAt(25);


char at 0th index:- t
char at 4th index:- n
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 25
 at java.lang.String.charAt(

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0436 seconds.