Junior — Middle
How to concatenate five hundred strings into one string using programming?
sobes.tech AI
Answer from AI
To concatenate 500 lines into one string in Java, you can use the StringBuilder class, which is efficient for concatenating a large number of strings.
Example:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500; i++) {
sb.append("Line number ").append(i).append(" ");
}
String result = sb.toString();
System.out.println(result);
If the lines are already in a collection, such as a list, you can use String.join:
List<String> lines = ...; // list of 500 lines
String result = String.join("", lines);
Both methods allow efficiently concatenating many strings into one.