As far as correctness goes, this only works for Unicode Code Points in the Basic Multilingual Plane (those that can be represented by a single two byte char in Java). If you have characters outside of that, you will get two chars with an individual surrogate pair in each of them to represent a single real world character.
I use a for loop to iterate the string and use charAt() to get each character to examine it. Since the String is implemented with an array, the charAt() method is a constant time operation.
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char
}
That's what I would do. It seems the easiest to me.
Answer from jjnguy on Stack OverflowAs far as correctness goes, this only works for Unicode Code Points in the Basic Multilingual Plane (those that can be represented by a single two byte char in Java). If you have characters outside of that, you will get two chars with an individual surrogate pair in each of them to represent a single real world character.
I use a for loop to iterate the string and use charAt() to get each character to examine it. Since the String is implemented with an array, the charAt() method is a constant time operation.
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char
}
That's what I would do. It seems the easiest to me.
Two options
for(int i = 0, n = s.length() ; i < n ; i++) {
char c = s.charAt(i);
}
or
for(char c : s.toCharArray()) {
// process c
}
The first is probably faster, then 2nd is probably more readable.
Beginner Help: Iterate for each character in a string
java - Repeating a string using a for loop - Stack Overflow
In Java, is it possible to check string startsWith with a List? - Oracle Forums
returning a String in an if else loop in Java
Videos
I am struggling to wrap my head around the for each loop and how it can be applied through iteration of a defined string.
I need to step through the string character by character and for each character use a char variable to hold the value of that character. The issue is that I am not sure how to do this.
Please can someone explain this in layman's terms or provide an example of how I can achieve this?
Zero-based index
You are getting the error because the value of i is going beyond the last index available in Hello. The last index in Hello is "Hello".length() - 1 whereas the value of i is going beyond this value because of your loop terminating condition:
i < string.length() * num;
By the way, if you want to repeat Hello 3 times, you should do it as
for(int i = 0; i < num; i ++){
System.out.print(string);
}
Demo:
public class Main {
public static void main(String[] args) {
final String string = "Hello";
final int num = 3;
for (int i = 0; i < num; i++) {
System.out.print(string);
}
}
}
String#repeat
With Java 11+, you can do it without using a loop by using String#repeat:
System.out.println(string.repeat(num));
Demo:
public class Main {
public static void main(String[] args) {
final String string = "Hello";
final int num = 3;
System.out.println(string.repeat(num));
}
}
Just keep it simple
String string = "Hello";
int num = 3;
for (int i = 0; i < num; i++) {
System.out.println(string);
}
If you want to have your result in a new String you can just do this :
String string = "Hello";
int num = 3;
String res = "";
for (int i = 0; i < num; i++) {
res += string;
}
System.out.println(res);