Java Program to access character of a string



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements