STRING INSTANCE METHODS
Java String charAt() method
charAt() returns one char character from a String string at the specified index. Java string indices start at 0.
Retrieving your first character
String zodis = "Java";
char raide = zodis.charAt(1);
System.out.println(raide);a
- The variable
zodisstores the text Java. charAt(1)requests the character at index1.- The index
0is the letterJ, so index1is the firsta. - The method returns
char, notString.
Why is charAt() an instance method?
charAt() is called on a specific string because it reads a character from that string:
char raide = zodis.charAt(1);
zodis is the object whose character is read. We therefore do not write String.charAt(...); the method needs a specific String value.
Method syntax
public char charAt(int index)
| Part | Meaning |
|---|---|
| Calling object | zodis in the line zodis.charAt(1) |
| Parameter | index – the character's position in the string |
| Return type | char |
| Valid indices | From 0 to length() - 1 |
| Error | StringIndexOutOfBoundsExceptionwhen the index is outside the string's bounds |
Indices start at zero
String zodis = "Java";
System.out.println(zodis.charAt(0));
System.out.println(zodis.charAt(1));
System.out.println(zodis.charAt(2));
System.out.println(zodis.charAt(3));J
a
v
a
The text Java has four characters, but its last index is 3, not 4. This is because the first character has index 0.
First and last character
String tekstas = "Programavimas";
char pirma = tekstas.charAt(0);
char paskutine = tekstas.charAt(tekstas.length() - 1);
System.out.println(pirma);
System.out.println(paskutine);P
s
- The first character is always at index
0if the string is not empty. length()returns the character count.- The last character's index is one less than the length:
length() - 1. - This principle is often used to check endings, trailing symbols or the final letter of input.
Traversing text with a loop
String tekstas = "kodas";
for (int i = 0; i < tekstas.length(); i++) {
System.out.println(i + ": " + tekstas.charAt(i));
}0: k
1: o
2: d
3: a
4: s
for loop starts at 0 and continues while i is less than tekstas.length(). This condition prevents going beyond the last character.
Counting a character in text
String tekstas = "programavimas";
int kiekis = 0;
for (int i = 0; i < tekstas.length(); i++) {
if (tekstas.charAt(i) == 'a') {
kiekis++;
}
}
System.out.println(kiekis);3
Here, charAt(i) retrieves one character on each iteration. If it equals 'a', the counter increases. Notice that a char literal uses single quotes.
An index cannot equal length()
String zodis = "Java";
System.out.println(zodis.charAt(4));StringIndexOutOfBoundsException
Java has length 4, but its valid indices are 0, 1, 2 and 3. Index 4 is already outside the string.
Negative indices are invalid too
String zodis = "Java";
System.out.println(zodis.charAt(-1));StringIndexOutOfBoundsException
Java String indices do not count from the end, so -1 does not mean the last character. Retrieve the last character using tekstas.charAt(tekstas.length() - 1).
Empty text has no characters
String tekstas = "";
System.out.println(tekstas.charAt(0));StringIndexOutOfBoundsException
An empty string has length 0, so it has no valid index. Before retrieving the first or last character, check !tekstas.isEmpty().
Common mistakes
- Forgetting that the first index is
0, not1. - Using this for the last character:
charAt(length())instead ofcharAt(length() - 1). - Trying to call
charAt()with a negative index. - Calling the method on an empty string without checking
isEmpty(). - Confusing
charandString:charAt()returns one character, not a string.
When should you use charAt()?
charAt() Use it to retrieve or check a specific character: the first letter, final symbol, a user-selected position or each character in a loop. For a substring of several characters, a better choice is substring().