Java String charAt() Method

charAt(int index):

Returns the char value at the specified index.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public char charAt(int index)
public char charAt(int index)
public char charAt(int index)

 

Note: An index ranges from 0 to length() – 1. If the index is negative or greater than length() – 1, it will throw IndexOutOfBoundsException. 

 

Java String charAt() Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class TestString{
String str = "www.w3schools.blog";
public void charAtTest(){
//Returns the character value at the specified index.
System.out.println(str.charAt(8));
}
}
public class StringCharAtExample {
public static void main(String args[]){
//creating TestString object
TestString obj = new TestString();
//method call
obj.charAtTest();
}
}
class TestString{ String str = "www.w3schools.blog"; public void charAtTest(){ //Returns the character value at the specified index. System.out.println(str.charAt(8)); } } public class StringCharAtExample { public static void main(String args[]){ //creating TestString object TestString obj = new TestString(); //method call obj.charAtTest(); } }
class TestString{
    String str = "www.w3schools.blog";
    
    public void charAtTest(){
        //Returns the character value at the specified index.
        System.out.println(str.charAt(8));
    }
}

public class StringCharAtExample {
    public static void main(String args[]){
        //creating TestString object
        TestString obj = new TestString();
        
        //method call
        obj.charAtTest();
    }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
h
h
h