Open In App

String charAt() Method in Java with Examples

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The String charAt() method returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() – 1.

If the index value is greater than or equal to the string length or negative number it returns stringIndexOutOfBoundsException

Example:

Java




// Java Program to demonstrate use of
// String charAt() Method
 
class Gfg {
    public static void main(String args[]) {
        // Define a string
        String s = "Java String charAt() example";
 
        // Retrieve and print the character at index 8
        char ch = s.charAt(8);
        System.out.println(ch);
 
        // Retrieve and print the character at index 24
        ch = s.charAt(24);
        System.out.println(ch);
    }
}


Output

i
m

Syntax

public char charAt(int index)

Parameters

  • index: Index of the character to be returned.

Returns

  • Returns the character at the specified position.

Exceptions

  • StringIndexOutOfBoundsException– If the index is negative or greater than the length of the String.

Java String charAt() Examples

The following examples demonstrate how to use the charAt() method in Java:

Example 1:

To show the working of the charAt() method 

Java




// Java program to demonstrate
// working of charAt() method
 
// driver class
class Gfg {
    // main function
    public static void main(String args[])
    {
        String s = "Welcome! to Geeksforgeeks Planet";
 
        char ch = s.charAt(3);
        System.out.println(ch);
 
        ch = s.charAt(0);
        System.out.println(ch);
    }
}


Output

c
W

Example 2:

The following program demonstrates the StringIndexOutOfBoundsException.

Java




// Java program to demonstrate
// working of charAt() method
 
// Driver class
class Gfg {
    // main function
    public static void main(String args[])
    {
        String s = "abc";
 
        char ch = s.charAt(4);
        System.out.println(ch);
    }
}


Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:658)
at Gfg.main(File.java:9)

String charAt() Method – Java Programs

Let’s see some coding problems and solve them with the String charAt() method in Java.

1. Accessing the First and Last Character using the charAt() Java method

Java




// Java Program to implement
// Accessing the First and Last Character
import java.io.*;
 
// driver class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
        int len = str.length();
 
        // First Element
        System.out.println("First Element: "
                           + str.charAt(0));
 
        // Last Element
        System.out.println("First Element: "
                           + str.charAt(len - 1));
    }
}


Output

First Element: G
First Element: s

2. Print Characters Presented at Odd Positions and Even Positions using charAt() Java method

Java




// Java Program to implement
// Print Characters Presented at
// Odd Positions and Even Positions
import java.io.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
 
        // Odd Positions
        System.out.println("Odd Positions");
        for (int i = 0; i < str.length(); i += 2) {
            System.out.print(str.charAt(i));
        }
 
        System.out.println("\n");
 
        // Even Positions
        System.out.println("Even Positions");
        for (int i = 1; i < str.length(); i += 2) {
            System.out.print(str.charAt(i));
        }
    }
}


Output

Odd Positions
GesoGes

Even Positions
ekfrek

3. Counting Frequency of a Character in a String using the Java charAt() method

Java




// Java Program to implement
// Counting Frequency of a Character
// in a String
import java.io.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
        int count = 0;
 
        for (int i = 0; i < str.length(); i++) {
            // Counting e in string
            if (str.charAt(i) == 'e')
                count++;
        }
 
        // printing occurrence
        System.out.println("Count the occurrence of e : "
                           + count);
    }
}


Output

Count the occurrence of e : 4

References

To know more about more String Methods refer to the article Java String Methods

Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the charAt function and its uses in Java.

The charAt method in Java is a fundamental function for string manipulation. With this guide, you can easily access the characters of a string using the charAt function.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads