String charAt() Method in Java with Example.



The charAt() method of the String class returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Example

Live Demo

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "This is tutorialspoint";
      // prints character at 1st location
      System.out.println(str.charAt(0));

      // prints character at 5th location i.e white-space character
      System.out.println(str.charAt(4));

      // prints character at 18th location
      System.out.println(str.charAt(17));
   }
}

Advertisements