Tutorials

Java String Methods


String charAt() method


In java charAt( ) method returns the index of the asked character.

Index of the String starts from 0.

 

If you enter index greater than the length of the String or negative index then StringIndexOutOfBoundException

 

Example:


package string;

public class charAtDemo {

      public static void main(String[] args) {

            String str = "Sunrise";  //index starts from 0

            char ch = str.charAt(2);
            System.out.println("Character at index: "+ ch);
      }

}

Similarly you can also check for character at other index as well.

 



Please Share this page
Views : 81
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!