The Java charAt method is one of the String Methods, which is to return the character at the specified index position. This article will show how to write string charAt in this Programming language with an example.
The basic syntax of the string charAt in Java Programming language is as shown below.
public char chatAt(int Index_Position) //In order to use in program String_Object.charAt(int Index_Position)
- String_Object: Please specify the valid String Object to find the character.
- Index_Position: Please specify the index position of the desired character.
The charAt Function will return the Character from the String_Object at the specified Index_Position. If we specify the Index position out of range, the chartAt Function will throw an error. The position of an index starts from 0, not 1.
Java string charAt Example
In this program, we will use the string charAt method to return the Character at the given index position. Within this example, we declared the String variable and assigned a value using the first statement.
The following three statements will use the Java string charAt, find Characters at index positions 0, 6, and 11, and store the characters in str1, str2, and str3 variables of type char.
NOTE: For the charAt String Method, you should count the space as One Character. Please check the Java codePointAt and Java string length articles.
In the next line, we use the length function to find the string length. In this code snippet, we subtract one from the string length. Because the length of a string will count from 1 to n, where the index position will start from 0 to n – 1.
The following Java System.out.println statements will print the output
package StringFunctions;
public class CharAtMethod {
public static void main(String[] args) {
String str = "Learn Java Tutorial";
char str1 = str.charAt(0);
char str2 = str.charAt(6);
char str3 = str.charAt(11);
char str4 = str.charAt(str.length()- 1);
System.out.println( "Charcter at Index position 0 = " + str1);
System.out.println( "Charcter at Index position 6 = " + str2);
System.out.println( "Charcter at Index position 11 = " + str3);
System.out.println( "Charcter at Last Index position = " + str4);
}
}

TIP: I suggest you refer to the Java format and Java substring functions from the Java String Methods.
charAt Example 2
In this string charAt program, we will ask the user to enter Y or N. The Java charAt program will display the message based on the user-entered character.
package StringFunctions;
import java.util.Scanner;
public class CharAtMethod2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("\nPlease Enter Y or N letter: ");
String letter = sc.next();
char ch = letter.charAt(0);
if (ch == 'y' || ch == 'Y') {
System.out.println("Hey!! Welcome to Library");
}
else {
System.out.println("Goodbye to Library");
}
}
}
Please Enter Y or N letter:
y
Hey!! Welcome to Library
Let us enter the different characters as the charAt argument
Please Enter Y or N letter:
N
Goodbye to Library
The first two statements will ask the user to enter Y or N character. Next, we assign the user-entered value to the letter variable.
For the ch variable, we are checking the character at the Zero index position using the string charAt method.
Next, we used the Java If Else Statement to check whether the character at index position zero is y / Y or not.
- If it y or Y System.out.println(“Hey!! Welcome to Library”); statement will be printed
- Else System.out.println(“Goodbye to Library”); statement will be printed.
We can use the lowercase method to convert user input to lower also but to make the code easy, we are going in the standard way. If the task is to compare two strings without considering their case. I mean, for case-insensitive comparison, we must use Java compareToIgnoreCase.
How to get the last character in a string using Java charAt?
As we all know, the string characters are accessed by their index position, and it starts from 0 to n -1. Therefore, we use the built-in string length() function to get the total number of characters in a string. Next,
- length – 1 returns the last character in a string.
- Length – 2 means the second-to-last character.
public class example {
public static void main(String[] args)
{
String food = "Fries";
int len = food.length();
System.out.println(food.charAt(len - 1));
System.out.println(food.charAt(len - 2));
System.out.println(food.charAt(len - 3));
}
}
s
e
i
Using Java charAt inside a loop
Similar to any other string function, we can use a charAt function inside any loop, such as a for loop or a while loop. In the example below, the for loop iterates over the string characters, and the charAt function extracts the character at each position.
TIP: Unlike other programming languages, we cannot use the index position to extract the string character. Something like str[0] is not possible in Java. Therefore, we must utilize the charAt() function.
public class example {
public static void main(String[] args)
{
String food = "Pizza";
for(int i = 0; i < food.length(); i++)
{
System.out.println(food.charAt(i));
}
}
}
P
i
z
z
a
Java String charAt vs toCharArray
Both charAt and toCharArray are built-in string functions to work with characters.
- charAt: It returns a single character from a given string based on the given index position. For example, str.charAt(0) returns the first character from a given string (str).
- toCharArray: It converts the given string into a character array. For example, char[] ch = str.toCharArray()
What is the difference between the Java charAt and codePointAt?
Both the built-in String charAt and codePointAt functions access individual (single) characters in a string based on their index position. However, the charAt() method returns the character, and codePointAt() returns the Unicode code point of the character at that index position.
In the following example, we use a for loop to iterate over the string characters and then use charAt and codePointAt functions to extract the characters and their Unicode values.
public class example {
public static void main(String[] args)
{
String food = "Burger";
for(int i = 0; i < food.length(); i++)
{
System.out.println(food.charAt(i)+ " = " +food.codePointAt(i));
}
}
}
B = 66
u = 117
r = 114
g = 103
e = 101
r = 114