Java String Wrap wordWrap(final String rawString, final int lineLength)

Here you can find the source of wordWrap(final String rawString, final int lineLength)

Description

word Wrap

License

Open Source License

Declaration

public static String[] wordWrap(final String rawString,
            final int lineLength) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by                            /

import java.util.ArrayList;

public class Main {
    public static String[] wordWrap(final String rawString,
            final int lineLength) {
        if (rawString == null) {
            return new String[] { "" };
        }//w w w .j  a v  a2  s.co  m
        if ((rawString.length() <= lineLength)
                && (!rawString.contains("\n"))) {
            return new String[] { rawString };
        }
        final char[] rawChars = (rawString + ' ').toCharArray();
        StringBuilder word = new StringBuilder();
        StringBuilder line = new StringBuilder();
        final ArrayList<String> lines = new ArrayList();
        int lineColorChars = 0;
        for (int i = 0; i < rawChars.length; i++) {
            final char c = rawChars[i];
            if (c == '\u00A7') {
                word.append('\u00A7' + (rawChars[(i + 1)]));
                lineColorChars += 2;
                i++;
            } else if ((c == ' ') || (c == '\n')) {
                if ((line.length() == 0) && (word.length() > lineLength)) {
                    for (final String partialWord : word.toString().split(
                            "(?<=\\G.{" + lineLength + "})")) {
                        lines.add(partialWord);
                    }
                } else if (((line.length() + word.length()) - lineColorChars) == lineLength) {
                    line.append(word);
                    lines.add(line.toString());
                    line = new StringBuilder();
                    lineColorChars = 0;
                } else if (((line.length() + 1 + word.length()) - lineColorChars) > lineLength) {
                    for (final String partialWord : word.toString().split(
                            "(?<=\\G.{" + lineLength + "})")) {
                        lines.add(line.toString());
                        line = new StringBuilder(partialWord);
                    }
                    lineColorChars = 0;
                } else {
                    if (line.length() > 0) {
                        line.append(' ');
                    }
                    line.append(word);
                }
                word = new StringBuilder();
                if (c == '\n') {
                    lines.add(line.toString());
                    line = new StringBuilder();
                }
            } else {
                word.append(c);
            }
        }
        if (line.length() > 0) {
            lines.add(line.toString());
        }
        if ((lines.get(0).length() == 0)
                || (lines.get(0).charAt(0) != '\u00A7')) {
            lines.set(0, "\u00A7f" + lines.get(0));
        }
        for (int i = 1; i < lines.size(); i++) {
            final String pLine = lines.get(i - 1);
            final String subLine = lines.get(i);

            final char color = pLine
                    .charAt(pLine.lastIndexOf('\u00A7') + 1);
            if ((subLine.length() == 0) || (subLine.charAt(0) != '\u00A7')) {
                lines.set(i, '\u00A7' + (color) + subLine);
            }
        }
        return lines.toArray(new String[lines.size()]);
    }
}

Related

  1. getWordWrappedText(int maxWidth, String... lines)
  2. getWrappedLine(String line, int lineLength)
  3. lineWrap(String text, int width, boolean shiftNewLines)
  4. wordWrap(String entry)
  5. wordWrap(String input, int width)
  6. wrapString(String str)
  7. wrapString(String text, int maxLength)