Java String charAt() Method Last Updated : 23 Dec, 2024 Comments Improve Suggest changes Like Article Like Report String charAt() method in Java 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 stringIndexOutOfBoundsExceptionExample 1: Java class Geeks { 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); } } Outputi mSyntax of charAt() Methodpublic char charAt(int index)Parameter: 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.Example 2: The following program demonstrating the Exception case as StringIndexOutOfBoundsException. Java class Geeks { // main function public static void main(String args[]) { String s = "abc"; char ch = s.charAt(4); System.out.println(ch); } } OutputException 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)Example 3: Accessing the First and Last Character Java import java.io.*; class Geeks { // 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)); } } OutputFirst Element: G First Element: sExample 4: Print Characters Presented at Odd Positions and Even Positions Java import java.io.*; class Geeks { 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)); } } } OutputOdd Positions GesoGes Even Positions ekfrekExample 5: Counting Frequency of a Character in a String Java import java.io.*; class Geeks { 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); } } OutputCount the occurrence of e : 4 Comment More infoAdvertise with us Next Article Java String charAt() Method N Niraj_Pandey Follow Improve Article Tags : Java Java-Strings Java-lang package Java-Functions Practice Tags : JavaJava-Strings Similar Reads Java String codePointAt() Method The codePointAt() method in Java is part of the String class. It is used to return the Unicode value of the character at the specified index in the string. This method is very useful when working with characters beyond the Basic Multilingual Plane (BMP), such as emojis or special symbols.Example 1: 3 min read Java String getBytes() Method In Java, the getBytes() method of the String class converts a string into an array of bytes. This method is useful for encoding the strings into binary format, which can then be used for file I/O, network transmission, or encryption. It can be used with the default character set or with a specified 2 min read Array setChar() method in Java The java.lang.reflect.Array.setChar() is an inbuilt method in Java and is used to change a specified char value to a specified index of a given object array. Syntax: Array.setChar(Object []array, int index, char value) Parameter: This method takes three parameters: array: This is an array of type Ob 3 min read Charset toString() method in Java with Examples The toString() method is a built-in method of the java.nio.charset returns a string which describes the charset involved. Syntax: public final String toString() Parameters: The function does not accepts any parameter. Return Value: The function returns a string describing this charset. Below is the 1 min read Character.offsetByCodePoints() Method in java The Character.offsetByCodePoints(CharSequence seq, int index, int codePointOffset) is an inbuilt method in java that returns the index within the given char sequence that is offset from the given index by codePointOffset code points. Unpaired surrogates within the text range given by index and codeP 2 min read java.lang.Character Class Methods | Set 1 java.lang.Character Class wraps the value of a primitive data type char to an object of datatype Character. This object contains a single field having the data type char. This class provides several methods regarding character manipulations like converting them from lowercase to uppercase. Character 6 min read java.lang.Character class - methods | Set 2 In Java, the Character Class wraps the value of a primitive data type char into an object of datatype Character. This object contains a single field having the data type char. This class provides several methods regarding character manipulations, like converting them from lowercase to uppercase. Cha 6 min read CharArrayWriter toString() method in Java with examples The toString() method of the CharArrayWriter class in Java converts the given input data to a string. Syntax: public String[] toString() Parameters: This method does not accept any parameter. Return Value: This method returns a copy of the input data. Below program illustrate the above method: Progr 1 min read Difference Between charAt() and substring() Method in Java In Java, the charAt() method of the String class is used to extract the character from a string. It returns the character at the specified index in the String. The substring() method is used to extract some portion from the actual String and the actual string remains the same as it is. After that, t 3 min read Java String codePoint() Method with Examples A Java string consists of a group of characters and each character is associated with a Unicode point value (alias ASCII value). So to get the Unicode point value of a character in a string we will use the codepoint() method. So in order to move further, we need to know what are the associated Unico 4 min read Like