Java Language Strings Getting the nth character in a String

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

String str = "My String";

System.out.println(str.charAt(0)); // "M"
System.out.println(str.charAt(1)); // "y"
System.out.println(str.charAt(2)); // " "
System.out.println(str.charAt(str.length-1)); // Last character "g"

To get the nth character in a string, simply call charAt(n) on a String, where n is the index of the character you would like to retrieve

NOTE: index n is starting at 0, so the first element is at n=0.



Got any Java Language Question?