split a string in java into equal length substrings while maintaining word boundaries

后端 未结 2 738
生来不讨喜
生来不讨喜 2020-12-03 09:06

How to split a string into equal parts of maximum character length while maintaining word boundaries?

Say, for example, if I want to split a string \"hello world\" i

2条回答
  •  误落风尘
    2020-12-03 09:13

    Non-regex solution, just in case someone is more comfortable (?) not using regular expressions:

    private String justify(String s, int limit) {
        StringBuilder justifiedText = new StringBuilder();
        StringBuilder justifiedLine = new StringBuilder();
        String[] words = s.split(" ");
        for (int i = 0; i < words.length; i++) {
            justifiedLine.append(words[i]).append(" ");
            if (i+1 == words.length || justifiedLine.length() + words[i+1].length() > limit) {
                justifiedLine.deleteCharAt(justifiedLine.length() - 1);
                justifiedText.append(justifiedLine.toString()).append(System.lineSeparator());
                justifiedLine = new StringBuilder();
            }
        }
        return justifiedText.toString();
    }
    

    Test:

    String text = "Long sentence with spaces, and punctuation too. And supercalifragilisticexpialidocious words. No carriage returns, tho -- since it would seem weird to count the words in a new line as part of the previous paragraph's length.";
    System.out.println(justify(text, 15));
    

    Output:

    Long sentence
    with spaces,
    and punctuation
    too. And
    supercalifragilisticexpialidocious
    words. No
    carriage
    returns, tho --
    since it would
    seem weird to
    count the words
    in a new line
    as part of the
    previous
    paragraph's
    length.
    

    It takes into account words that are longer than the set limit, so it doesn't skip them (unlike the regex version which just stops processing when it finds supercalifragilisticexpialidosus).

    PS: The comment about all input words being expected to be shorter than the set limit, was made after I came up with this solution ;)

提交回复
热议问题