大字符串在java中分成最大长度的行 [英] Large string split into lines with maximum length in java

查看:21
本文介绍了大字符串在java中分成最大长度的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 个字符.

I want to split this large string into lines and the lines should not content more than MAX_LINE_LENGTH characters in each line.

到目前为止我尝试了什么

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 ...

它会导致断词.我不要这个.而不是我想得到这样的输出:

It causes breaking of words. I don't want this. Instead of I want to get output like this:

[THESE TERMS AND , CONDITIONS OF , SERVICE (the Terms) , ARE A LEGAL AND B ...

又添加了一个条件:如果字长大于 MAX_LINE_LENGTH,则应拆分该字.

One more condition added : If a word length is greater than MAX_LINE_LENGTH then the word should get split.

解决方案应该不需要外部罐子的帮助.

And solution should be without helping of external jars.

推荐答案

只需一个词一个词地遍历字符串,只要一个词超过限制就中断.

Just iterate through the string word by word and break whenever a word passes the limit.

public String addLinebreaks(String input, int maxLineLength) {
    StringTokenizer tok = new StringTokenizer(input, " ");
    StringBuilder output = new StringBuilder(input.length());
    int lineLen = 0;
    while (tok.hasMoreTokens()) {
        String word = tok.nextToken();

        if (lineLen + word.length() > maxLineLength) {
            output.append("
");
            lineLen = 0;
        }
        output.append(word);
        lineLen += word.length();
    }
    return output.toString();
}

我只是徒手输入的,您可能需要稍微推动一下才能使其编译.

I just typed that in freehand, you may have to push and prod a bit to make it compile.

错误:如果输入中的单词比 maxLineLength 长,它将被附加到当前行而不是它自己的太长的行.我假设您的行长度大约为 80 或 120 个字符,在这种情况下,这不太可能成为问题.

Bug: if a word in the input is longer than maxLineLength it will be appended to the current line instead of on a too-long line of its own. I assume your line length is something like 80 or 120 characters, in which case this is unlikely to be a problem.

这篇关于大字符串在java中分成最大长度的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆