Java String Split by Line splitLines(String str, int width)

Here you can find the source of splitLines(String str, int width)

Description

split Lines

License

Open Source License

Declaration

private static String[] splitLines(String str, int width) 

Method Source Code


//package com.java2s;
/*/*www  .j a va  2  s  .  c  o m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * See LICENSE.txt included in this distribution for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at LICENSE.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static String[] splitLines(String str, int width) {
        List<String> res = new ArrayList<>();
        StringBuilder b = new StringBuilder();

        int llen = 0;
        int i = 0;
        while (i < str.length()) {
            int wlen = nextTestWhitespaceLength(false, str, i);
            if (wlen > 0) {
                String word = str.substring(i, i + wlen);
                if (llen < 1) {
                    b.append(word);
                    llen = word.length();
                } else if (llen + 1 + wlen <= width) {
                    b.append(" ");
                    b.append(word);
                    llen += word.length() + 1;
                } else {
                    res.add(b.toString());
                    b.setLength(0);
                    b.append(word);
                    llen = word.length();
                }
                i += wlen;
            }

            int slen = nextTestWhitespaceLength(true, str, i);
            i += slen;
        }
        if (b.length() > 0) {
            res.add(b.toString());
            b.setLength(0);
        }

        return res.stream().toArray(String[]::new);
    }

    private static int nextTestWhitespaceLength(boolean match, String str, int off) {
        int i = 0;
        while (off + i < str.length()) {
            int cp = Character.codePointAt(str, off + i);
            if ((Character.isWhitespace(cp) || Character.isISOControl(cp)) != match) {
                return i;
            }
            i += Character.charCount(cp);
        }
        return str.length() - off;
    }
}

Related

  1. splitLines(final String string)
  2. splitLines(String raw, int limit)
  3. splitLines(String S)
  4. splitLines(String s)
  5. splitLines(String self)
  6. splitlines(String text)
  7. splitLines(String text)
  8. splitLines(String text)
  9. splitLogLine(final String logLine)