for each loop thorugh string java code example
Example 1: java iterate over a string
String scientistName = "Isaac Newton";
for (int i = 0; i < scientistName.length(); i++) {
System.out.print(scientistName.charAt(i) + " ");
}
String str = "strings are not primitive types!";
for (char ch : str.toCharArray()) {
System.out.print(scientistName.charAt(i) + " ");
}
}
Example 2: iterate over string java
String str = "hello";
for (char c : str.toCharArray())
Example 3: java foreach char
for (char ch: "xyz".toCharArray()) {
System.out.println(ch);
}