我正在寻找一个库/API,它通过空格和换行符分隔字符串,以便对超出给定限制的行应用合理的换行。例如,如果我将要查找的格式化程序设置为列宽 10 并添加以下字符串:
String s1 = "The quick brown fox jumped over the lazy dog.\n";
String s2 = "The quick\nbrown fox jumped over\nthe lazy dog.";
我期望输出类似于:
The quick
brown fox
jumped
over the
lazy dog.
The quick
brown fox
jumped
over
the lazy
dog.
即应尊重输入文本已标记的换行符,并在必要时添加其他换行符。
这是我自己快速而笨拙的实现,以说明我的期望的原始基线: Column Formatter Whip-Up
最佳答案
尝试 Apache commons-lang 库中的 WordUtils.wrap(String, int) 方法。
关于java - 用于在给定宽度的列中打印字符串中的文本以进行控制台输出的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8121761/