I need to simply wrap long text to display it in a few lines. I tried the default snippet:
String s = "Español texto aquí";
StringBuilder sb = new StringBuilder(s);
int i = 0;
while (i + 20 < sb.length() && (i = sb.lastIndexOf(" ", i + 20)) != -1) {
sb.replace(i, i + 1, "\n");
}
System.out.println(sb.toString());
but with Spanish text I get wrong symbols in output. How do I wrap non-English text?
Java has one of the best handling of UTF-8 text I’ve seen (it internally uses UTF-16).
The only thing dependant on OS or encoding here is the line separator character you’re using. You might try
or
instead of
\n.If that doesn’t help either, the problem is probably in the console/terminal you’re reading the result from, not the code. Make sure the source .java file is saved as UTF-8, try redirecting the output to a file and not console.