Roman
Roman

Reputation: 2079

Java wrap UTF-8 text?

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?

Upvotes: 1

Views: 302

Answers (3)

Eltan Hajiyev
Eltan Hajiyev

Reputation: 455

There is a ready solution for that.

https://mvnrepository.com/artifact/org.apache.commons/commons-text

String s = "Español texto aquí texto aquí Español texto aquí";
WordUtils.wrap(s, 20);

Upvotes: 0

Petr Janeček
Petr Janeček

Reputation: 38444

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

System.lineSeparator()  // Java 7 only

or

System.getProperty("line.separator")

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.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200206

String s = "Español texto aquí texto aquí 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());

prints

Español texto aquí
texto aquí Español
texto aquí

Now, I don't see anything wrong with that.

Upvotes: 5

Related Questions