String input = "THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this site, www.nationalgeographic.com, which includes but is not limited to products, software and services offered by way of the website such as the Video Player, Uploader, and other applications that link to these Terms (the Site). Please review the Terms fully before you continue to use the Site. By using the Site, you agree to be bound by the Terms. You shall also be subject to any additional terms posted with respect to individual sections of the Site. Please review our Privacy Policy, which also governs your use of the Site, to understand our practices. If you do not agree, please discontinue using the Site. National Geographic reserves the right to change the Terms at any time without prior notice. Your continued access or use of the Site after such changes indicates your acceptance of the Terms as modified. It is your responsibility to review the Terms regularly. The Terms were last updated on 18 July 2011.";
//text copied from http://www.nationalgeographic.com/community/terms/
我想将此大字符串拆分为行,并且每行中的行不应超过MAX_LINE_LENGTH个字符。在java中分割成最大长度的行中的大字符串
我试过到目前为止
int MAX_LINE_LENGTH = 20;
System.out.print(Arrays.toString(input.split("(?<=\\G.{MAX_LINE_LENGTH})")));
//maximum length of line 20 characters
输出:
[THESE TERMS AND COND, ITIONS OF SERVICE (t, he Terms) ARE A LEGA, L AND B ...
它会导致破字的。我不想要这个。 而不是我想要得到的输出是这样的:
[THESE TERMS AND , CONDITIONS OF , SERVICE (the Terms) , ARE A LEGAL AND B ...
一个条件补充说: 如果一个字长度大于MAX_LINE_LENGTH大于这个词应该得到分流。
解决方案应该没有外部罐子的帮助。
可能重复http://stackoverflow.com/questions/4055430/java-code-for-wrapping-text-lines-to-a -max-line-width) – hammar
@hammer - 我的客户不希望我使用任何外部jar文件。没有任何外部jar文件,我没有在你提到的那个线程中得到任何解决方案。 – Abhishek
是啊......就这样做了。 – Abhishek