Java Program to get a character located at the String's specified index


Use the charAt() method in Java to get a character located at a specified index.

Let’s say the following is our string.

String str = "Laptop...";

Finding character at 3rd position.

str.charAt(2);

The following is the final example.

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       String str = "Laptop...";
       System.out.println("String: "+str);
       System.out.println("Character at position 3 = " +str.charAt(2));
    }
}

Output

String: Laptop...
Character at position 3 = p

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements