The Java String charAt() method returns the character at the specified index. The index value should lie between 0 and length() – 1. Let us check more points about the String charAt() method in Java.
Syntax of charAt() Java method
public char charAt(int index)
Parameters
- index: Index of the character to be returned.
Return Type
- Returns the character at the specified position.
Exceptions
- StringIndexOutOfBoundsException– If the index is negative or greater than the length of the String.
Example of Java String charAt()
The following examples demonstrate how to use charAt() method:
Example 1:
To show the working of the charAt() method
Java
class Gfg {
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);
}
}
|
Example 2:
The following program demonstrates the StringIndexOutOfBoundsException.
Java
class Gfg {
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)
Programs for charAt Java Method
1. Accessing the First and Last Character using the charAt() Java method
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
int len = str.length();
System.out.println( "First Element: "
+ str.charAt( 0 ));
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
System.out.println( "Odd Positions" );
for ( int i = 0 ; i < str.length(); i += 2 ) {
System.out.print(str.charAt(i));
}
System.out.println( "\n" );
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 Java charAt() method
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == 'e' )
count++;
}
System.out.println( "Count the occurrence of e : "
+ count);
}
}
|
Output
Count the occurrence of e : 4
References
To know more about more String classes refer to the article String Class in Java.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
31 Jul, 2023
Like Article
Save Article