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



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 : 40