← All languages
Java method of the day
Random

String.charAt

Return the character at a specified index in a string.

Description

The charAt() method returns the char value at the specified index in a string. An index ranges from 0 to length() - 1. The first character is at index 0, the last at length() - 1.

If the index argument is negative or not less than the length of this string, a StringIndexOutOfBoundsException is thrown. Always validate the index before calling this method if the input is dynamic.

This method is commonly used for character-by-character processing of strings, such as parsing, validation, or transformation tasks where each character must be examined individually.

Arguments

NameDescriptionOptional
index The index of the character to return. No

Example

String s = "Hello";
char c = s.charAt(0); // 'H'
char d = s.charAt(4); // 'o'

Reference