Java Program to access character of a string

Java 8Object Oriented ProgrammingProgramming

To access character of a string in Java, use the charAt() method. The position is to be added as the parameter.

Let’s say we have the following string −

String str = "laptop";

Let’s find the character at the 4th position using charAt() method.

str.charAt(3));

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);
      // finding character at 4th position
      System.out.println("Character at 4th position: "+str.charAt(3));
   }
}

Output

String: laptop
Character at 4th position: t
raja
Published on 06-Dec-2018 16:31:23
Advertisements