+charAt(index: int) return type char

The str.CharAt(index) method can be used to retrieve a specific character in a string str, where the index of between 0 and str.length()-1.

import java.lang.String;
class charAt{
                    public static void main(String[] arg){ 
                          String str="Welcome to Java";
                          System.out.println("str.charAt(0): "+str.charAt(0));
                         }
                     }

Output:  str.charAt(0): W

Here:  str.charAt(0) returns the character W, as show in output and in fig given below.

Attempting to access characters in a string str out of bounds is common programming error. To avoid it, make sure that you do not use an index beyond str.length()-1.
For exmaple:-

import java.lang.String;
class charAt{
                    public static void main(String[] arg){ 
                          String str="Welcome to Java";
                          System.out.println("str.charAt(0): "+str.charAt(str.length()));
                         }
                     }
str.charAt(str.length()) would cause a StringIndexOutofBoundException,

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons