Java String charAt() Method

charAt(int index):

Returns the char value at the specified index.

Syntax:

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

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

h

 

 

Please follow and like us:
Content Protection by DMCA.com