To iterate over the characters of a string in Java, you can use a for loop or the for-each loop.
Here is an example of how you can use a for loop to iterate over the characters of a string and print them:
String str = "Hello World";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
System.out.println(c);
}
This will print each character of the string on a separate line.
Here is an example of how you can use a for-each loop to achieve the same result:
String str = "Hello World";
for (char c : str.toCharArray()) {
System.out.println(c);
}
You can also use the Iterator class to iterate over the characters of a string, like this:
String str = "Hello World";
Iterator<Character> iterator = str.chars().mapToObj(c -> (char) c).iterator();
while (iterator.hasNext()) {
char c = iterator.next();
System.out.println(c);
}
Keep in mind that the String class is immutable, which means that the characters of a string cannot be modified directly. If you need to modify the characters of a string, you can use the StringBuilder class or the String.replace method.