If you want to display or get any one character from a string, java provides a method .charAt(index) [this returns a character from the specified index], let’s see the implementation
index should be with in the string length range, else will get exception java.lang.StringIndexOutOfBoundsException
public static void main(String[] args) { String myStr = new String("Welcome"); System.out.println("Character ay index 0 - " + myStr.charAt(0)); System.out.println("Character at index 1 - " + myStr.charAt(1)); }
Output
Character ay index 0 - W Character at index 1 - e
String “Welcome” is of length 7, so the index should be from 0 to 6.