98. Using 'charAt( )' method to retrieve the character from string text

You can use charAt( ) method to retrieve the character at a specified index with in a string.


String myString1 = "abcdefghijklmnopqrstuvwxyz";


Lets implement this on Eclipse IDE:

1. Create 'CharAtDemo' class under any project as shown below:

2. Save and Run the 'CharAtDemo' class
3. Observe that the output is displayed in the console as shown below:

Download this project:

Click here to download this project containing 'CharAtDemo' class file used in this post (You can download the project and import into Eclipse IDE on your machine)

Please comment below to feedback or ask questions.

'String Arrays' topic will be explained in the next post.


Naveen said...

Arun I need one clarification

I have used the same programs as mentioned above and when i tried to
print the charAt 26 th place it gave z .... when the index starts at o for A
then then the index ends at 25 for z right but i got the output as z for 26 ..
Can you let me know the logic here

Arun Motoori said...

@Naveen - I have tried the below program as you mentioned and got the exception instead of getting z as you mentioned.

public class Test {

public static void main(String[] args) {

String str = "abcdefghijklmnopqrstuvwxyz";



Output -> Run time Exception as expected with text

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 26
at java.lang.String.charAt(
at Test.main(

Please double check the String text that you provided as input to the program.