What is Java String charAt() method?
The Java string class charAt() method returns the character at the specified index of a string. This specified index is provided as a parameter and it ranges from 0 to n-1, where n represents the length of the string. Syntax
public char charAt(int index)
Parameters
The charAt() method receives a positive integer because the length is never below 0.
Returns
It always returns a char value of the specified index but if parameter value is negative or above the length of the string then its throws an exception.
Java String charAt() Method Examples
class Main {
public static void main(String[] args) {
String str = "Java charAt() method example";
// picking the A character of the string str at index 9 using charAt() method
System.out.println(str.charAt(9));
// picking the ( character of the string str at index 11 using charAt() method
System.out.println(str.charAt(11));
// picking the ) character of the string str at index 12 using charAt() method
System.out.println(str.charAt(12));
}
}
Output
БA
(
)
GO TO FULL VERSION