Looping Over Characters
Process each char in a string.
Strings Are Made of Chars
A String is a sequence of characters. Each character sits at a position called an index, starting at 0.
In "Java", index 0 is 'J', index 1 is 'a', and so on. Looping lets you visit each char in order.
String word = "Java";
// indexes: 0=J 1=a 2=v 3=acharAt Gets One Character
The charAt(index) method returns the char at a given position in a String.
Indexes run from 0 to length() - 1. Asking for an index outside that range throws an error.
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.charAt(0));
System.out.println(word.charAt(3));
}
}All lessons in this course
- The char Primitive
- Character Helper Methods
- Looping Over Characters
- Counting and Filtering Chars