我需要简单地换行长文本以将其显示在几行中。我尝试了默认代码段:
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());
但是对于西类牙语文本,我在输出中得到了错误的符号。如何换行非英文文本?
最佳答案
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());
打印
Español texto aquí
texto aquí Español
texto aquí
现在,我看不出有什么不妥。
关于Java 换行 UTF-8 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11197027/