I am trying to wrap text based on a width of 10 characters. After looking at other questions, I have the following:
StringBuilder sb = new StringBuilder(s);
int i = 0;
while (i + 10 < sb.length() && (i = sb.lastIndexOf(" ", i + 10)) != -1) {
sb.replace(i, i + 1, "\n");
}
System.out.println(sb.toString());
This works until a word in my string is longer than the specified width. When this occurs, the rest of the string is printed on line line instead of still following the line width rule. Any ideas? I tried an "else" with a separate condition but I couldn't get it to work. Apologies if this seems rather trivial.