loop over string in 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: java loop through string
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
}
Example 3: Java loop over string
String s = "Techie Delight";
char[] chars = s.toCharArray();
for (char ch: chars) {
System.out.print(ch);
}