Java String charAt() Method

Java String charAt() Method | In this section, we will discuss the charAt() method available in the Java String class which is used to return the character at the specified index. The charAt() method takes an integer value as a parameter i.e. it takes the index as the parameter. The index starts from 0 and if the index is greater than the length of the string then charAt() method throws IndexOutOfBoundException.

The method details of the charAt() are as follows:- public char charAt(int index)
Parameter:- an integer parameter index where the character needs to be returned.
Return type:- character.
Exception: IndexOutOfBoundException when the index is negative or greater than the length of the string.

String charAt() in Java

Now let us see the code to understand how exactly charAt() works. In the below program we retrieve different characters from the same string using different character variables.

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

      char res1 = str.charAt(8);
      char res2 = str.charAt(0);
      char res3 = str.charAt(5);

      System.out.println(res1);
      System.out.println(res2);
      System.out.println(res3);

      System.out.println(str.charAt(3));
      System.out.println(str.charAt(6));
   }
}

Output:-

r
K
r
w
o

Let us see another example of the Java String charAt() method. After getting the character at a specific index from the given string using charAt() we can store it to a char type variable and use them in the program.

public class Main {
   public static void main(String args[]) {
      String str = "Welcome to KnowProgram";

      char res = str.charAt(3);
      System.out.println(res);

      res = str.charAt(0);
      System.out.println(res);
   }
}

Output:-

c
W

Java String charAt() Exception

This program shows the exception thrown by the charAt() method. If we pass any negative number or the number greater than or equal to the length of the string then charAt() method throws StringIndexOutOfBoundsException. It is an unchecked exception therefore it is not necessary to handle it using try-catch.

public class Main {
   public static void main(String args[]) {
      String str = "KnowProgram";
      System.out.println("String length: " + str.length());
      System.out.println(str.charAt(-5));
      // System.out.println(str.charAt(11));
      // System.out.println(str.charAt(30));
   }
}

Output:-

String length: 11
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -5
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Main.main(Main.java:5)

Using string class length() we can find the length of the string. With the help of the charAt() method, we can solve multiple problems related to the string. Some examples are:- Find vowels in the given string, find consonants in the given string, find the number of occurrences of a character in the given string, and traverse/iterate through each character of the string.

Iterate Through Each Character of The String using charAt() in Java

To iterate through the character of the string we can use a loop from 0 to till the length of the string, and then get the character from the string using the charAt() method. Let us see an example through for loop.

public class Main {
   public static void main(String args[]) {
      String string = "KnowProgram";
      for(int i=0; i<string.length(); i++) {
         System.out.print(string.charAt(i) + " ");
      }
   }
}

Output:-

K n o w P r o g r a m

Java Program to Print Vowels in a Given String

public class Main {
   public static void main(String args[]) {
      String string = "Hello World!";
      System.out.println("Vowels in the given string are: ");
      for (int i = 0; i < string.length(); i++) {
         char ch = string.charAt(i);
         if (ch == 'a' || ch == 'e' || ch == 'i' || 
            ch == 'o' || ch == 'u' || ch == 'A' || 
            ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
            System.out.print(string.charAt(i) + " ");
      }
   }
}

Output:-

Vowels in the given string are:
e o o

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.