Java String charAt()

The method charAt(int index) returns the character at the specified index. The index value should lie between 0 and length()-1. For e.g. s.charAt(0) would return the first character of the string “s”. It throws IndexOutOfBoundsException if the index is less than zero or greater than equal to the length of the string (index<0|| index>=length()).

Internal implementation
public char charAt(int index) { if ((index < 0) || (index >= value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; }
Signature

The signature of string charAt() method is given below:

public char charAt(int index)
Parameter

index : index number, starts with 0

Returns

A char value

Specified by

CharSequence interface, located inside java.lang package.

Throws

StringIndexOutOfBoundsException : if index is negative value or greater than this string length.

Example:

In this example we are fetching few characters of the input string using charAt() method.

public class Test1 { public static void main(String args[]) { String str = "Welcome to java programming"; char ch1 = str.charAt(11); char ch2 = str.charAt(12); char ch3 = str.charAt(13); char ch4 = str.charAt(14); System.out.println("Character at 11th index is: "+ch1); System.out.println("Character at 12h index is: "+ch2); System.out.println("Character at 13th index is: "+ch3); System.out.println("Character at 14th index is: "+ch4); } }


Output:
Character at 11th index is: j Character at 12th index is: a Character at 13th index is: v Character at 14th index is: a
Java String charAt() method example
public class Test { public static void main(String args[]) { String name="c programming coding"; char ch=name.charAt(7);//returns the char value at the 7th index System.out.println(ch); } }


Output:
a
StringIndexOutOfBoundsException with charAt()

See the example of charAt() method where we are passing greater index value. In such case, it throws StringIndexOutOfBoundsException at run time.

public class Test { public static void main(String args[]) { String name="c programming coding"; char ch=name.charAt(30);//returns the char value at the 30th index System.out.println(ch); } }


Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 30 at java.lang.String.charAt(String.java:687) at Test1.main(Test1.java:6)
Java String charAt() Example 3

See a simple example where we are accessing first and last character from the provided string.

public class Test1 { public static void main(String[] args) { String str = "Welcome to c programming coding"; int strLength = str.length(); // Fetching first character System.out.println("Character at 0 index is: "+ str.charAt(0)); // The last Character is present at the string length-1 index System.out.println("Character at last index is: "+ str.charAt(strLength-1)); } }


Output:
Character at 0 index is: W Character at last index is: g
Java String charAt() Example 4

See an example where we are accessing all the elements present at odd index.

public class Test1 { public static void main(String[] args) { String str = "Welcome to c programming coding"; for (int i=0; i<=str.length()-1; i++) { if(i%2!=0) { System.out.println("Char at "+i+" place "+str.charAt(i)); } } } }


Output:
Char at 1 place e Char at 3 place c Char at 5 place m Char at 7 place Char at 9 place o Char at 11 place c Char at 13 place p Char at 15 place o Char at 17 place r Char at 19 place m Char at 21 place i Char at 23 place g Char at 25 place c Char at 27 place d Char at 29 place n
Java String charAt() Example 5

See an example where we are counting frequency of a character in the string.

public class Test1 { public static void main(String[] args) { String str = "Welcome to c programming coding"; int count = 0; for (int i=0; i<=str.length()-1; i++) { if(str.charAt(i) == 'c') { count++; } } System.out.println("Frequency of c is: "+count); } }


Output:
Frequency of c is: 3



Instagram