string.charAt() Returns the character present at the input position of the string.
Syntax
string.charAt(input)
input is an integer staring from 0 ( first position )
Here are some examples with output.
String my_str="Welcome to plus2net";
System.out.println(my_str.charAt(0)); // W
System.out.println(my_str.charAt(3)); // c
System.out.println(my_str.charAt(15)); // 2
Note that first position of the string starts from 0. So what happens if we give negative integer as input or any position more than the length of the string?
That will raise exception.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Examples by using charAt()
We will display a strting by using each char in a line. We are using one for loop here.