Java String charAt() method – TutorialAndExample

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1.

Syntax:

public char charAt (int index)

Parmeters

It accepts only one parameter that is the index of the character.

Returns

charAt() method returns char value present at the specified index in the string.

Throws:

The method throws IndexOutOfBoundsException if index is negative or greater than the length of the string.

Example 1:

public class CharAtEx1 {
    public static void main(String args[]) {
        String str = "Welcome to tutorialandexample ";
          char ch1 = str.charAt(0);
          char ch2 = str.charAt(3);
          char ch3 = str.charAt(5);
          char ch4 = str.charAt(8);
        char ch5 = str.charAt(15);
          char ch6 = str.charAt(17);
          System.out.println("Character at 0 index is: "+ch1);
          System.out.println("Character at 3rd index is: "+ch2);
          System.out.println("Character at 5th index is: "+ch3);
          System.out.println("Character at 8th index is: "+ch4);
        System.out.println("Character at 15th index is : "+ch5);
        System.out.println("Character at 17th index is : "+ch6);
    }
}

output :

Character at 0 index is: W
Character at 3rd index is: c
Character at 5th index is:
Character at 8th index is: t
Character at 15th index is : r
Character at 17th index is : a

Example 2 :- StringIndexOutOfBoundsException with charAt()

public class CharAtEx2
{
    public static void main(String args[])
    { 
String TE="tutorialandexample";
//returns the char value at the 19th index 
char c=TE.charAt(19);
System.out.println(c); 
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
19at java.lang.String.char
At(String.java:658)at CharAtEx1.main(CharAtEx1.java:7)

Example 3 :

public class CharAtEx3{
public static void main(String[] args) { 
        String str = "Welcome to tutorialandexample portal";         
        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 t
Char at 13 place t
Char at 15 place r
Char at 17 place a
Char at 19 place a
Char at 21 place d
Char at 23 place x
Char at 25 place m
Char at 27 place l
Char at 29 place
Char at 31 place o
Char at 33 place t
Char at 35 place l

Example 4:

public class CharAtEx4
{
public static void main(String[] args)
    {
        String str = "Welcome to tutorialandexample !";
        System.out.println("Here String is = " + str);
        // Get the character at positions 0 and 10.
        int index1 = str.charAt(5);
        int index2 = str.charAt(29);
        char ch = str.charAt(17);
        // Print out the results.
        System.out.println("The character at position 5 is " +
            (char)index1);       
        // Here returns the char value at the 29th index i.e. is empty space 
        System.out.println("The character at position 29 is " +
            (char)index2);
        System.out.println("The Character at 17th index is : "+ch);
    }
}

Output:

Here String is = Welcome to tutorialandexample !
The character at position 5 is m
The character at position 29 is
The Character at 17th index is : a

Example 5:

public class Freq_test {
public static void main(String[] args) {
          String str = "Tutorial times";
          int count;
          for(int i=0;i<str.length();i++)
          {
                   count = 0;
                   for(int j=0;j<str.length();j++)
                   {
                             if(str.charAt(i)==str.charAt(j))
                             {
                                      count++;
                             }                          
                   }
                   System.out.println(str.charAt(i)+" occurs "+count+" times");
          }
}
}

Output:

T occurs 1 times
u occurs 1 times
t occurs 2 times
o occurs 1 times
r occurs 1 times
i occurs 2 times
a occurs 1 times
l occurs 1 times
occurs 1 times
t occurs 2 times
i occurs 2 times
m occurs 1 times
e occurs 1 times
s occurs 1 times