Java Word Wrap wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)

Here you can find the source of wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)

Description

Wrap multi-line strings (and get the individual lines).

License

Apache License

Parameter

Parameter Description
original the original string to wrap
width the maximum width of lines
breakIterator breaks original to chars, words, sentences, depending on what instance you provide.
removeNewLines if <code>true</code>, any newlines in the original string are ignored

Return

the lines after wrapping

Declaration

public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator,
        boolean removeNewLines) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.BreakIterator;

import java.util.StringTokenizer;

public class Main {
    /** Wrap multi-line strings (and get the individual lines).
    * @param original  the original string to wrap
    * @param width     the maximum width of lines
    * @param breakIterator breaks original to chars, words, sentences, depending on what instance you provide.
    * @param removeNewLines if <code>true</code>, any newlines in the original string are ignored
    * @return the lines after wrapping/*from   w ww  . j a v  a 2s.c  o m*/
    */
    public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator,
            boolean removeNewLines) {
        if (original.length() == 0) {
            return new String[] { original };
        }

        String[] workingSet;

        // substitute original newlines with spaces,
        // remove newlines from head and tail
        if (removeNewLines) {
            original = trimString(original);
            original = original.replace('\n', ' ');
            workingSet = new String[] { original };
        } else {
            StringTokenizer tokens = new StringTokenizer(original, "\n"); // NOI18N
            int len = tokens.countTokens();
            workingSet = new String[len];

            for (int i = 0; i < len; i++) {
                workingSet[i] = tokens.nextToken();
            }
        }

        if (width < 1) {
            width = 1;
        }

        if (original.length() <= width) {
            return workingSet;
        }

        widthcheck: {
            boolean ok = true;

            for (int i = 0; i < workingSet.length; i++) {
                ok = ok && (workingSet[i].length() < width);

                if (!ok) {
                    break widthcheck;
                }
            }

            return workingSet;
        }

        java.util.ArrayList<String> lines = new java.util.ArrayList<String>();

        int lineStart = 0; // the position of start of currently processed line in the original string

        for (int i = 0; i < workingSet.length; i++) {
            if (workingSet[i].length() < width) {
                lines.add(workingSet[i]);
            } else {
                breakIterator.setText(workingSet[i]);

                int nextStart = breakIterator.next();
                int prevStart = 0;

                do {
                    while (((nextStart - lineStart) < width) && (nextStart != BreakIterator.DONE)) {
                        prevStart = nextStart;
                        nextStart = breakIterator.next();
                    }

                    if (nextStart == BreakIterator.DONE) {
                        nextStart = prevStart = workingSet[i].length();
                    }

                    if (prevStart == 0) {
                        prevStart = nextStart;
                    }

                    lines.add(workingSet[i].substring(lineStart, prevStart));

                    lineStart = prevStart;
                    prevStart = 0;
                } while (lineStart < workingSet[i].length());

                lineStart = 0;
            }
        }

        String[] s = new String[lines.size()];

        return (String[]) lines.toArray(s);
    }

    private static String trimString(String s) {
        int idx = 0;
        char c;
        final int slen = s.length();

        if (slen == 0) {
            return s;
        }

        do {
            c = s.charAt(idx++);
        } while (((c == '\n') || (c == '\r')) && (idx < slen));

        s = s.substring(--idx);
        idx = s.length() - 1;

        if (idx < 0) {
            return s;
        }

        do {
            c = s.charAt(idx--);
        } while (((c == '\n') || (c == '\r')) && (idx >= 0));

        return s.substring(0, idx + 2);
    }
}

Related

  1. wordWrap(String input, int width, Locale locale)
  2. wordWrap(String input, int width, Locale locale)
  3. wordWrap(String input, int width, Locale locale)
  4. wrapString(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
  5. wrapText(String s, int width, boolean noIterator)
  6. wrapText(String string, int theWrapLength)