Character indices

Edit on Github instead

Use Java's charAt(int) function to find the character at a specifix index.

Called on: a string

Arguments: the index at which you'd like the character

Returns: the character at the index passed as an argument


String book = "handbook";
book.charAt(4);                     // 'b'
book.charAt(0);                     // 'h'

If you pass index that's larger than the string is long, you'll get a StringIndexOutOfBoundsException:

String hello = "Hello!";
java.lang.StringIndexOutOfBoundsException: String index out of range: 29

Suggest changes

Related links

URL Topic Source