Java String Wrap wordWrap(String input, int width)

Here you can find the source of wordWrap(String input, int width)

Description

word Wrap

License

Open Source License

Declaration

public static List<String> wordWrap(String input, int width) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static List<String> wordWrap(String input, int width) {
        ArrayList<String> output = new ArrayList<>();
        final char[] chars = input.toCharArray();
        final int N = input.length();

        if (N < width) {
            return Arrays.asList(input.split("\n"));
        }//from  w w w .  j av  a2  s  .  c  o  m
        int prevLine = 0;
        while (true) {
            int newline = -1;
            int maxspace = -1;
            for (int i = 0; i <= Math.min(N - prevLine - 1, width); i++) {
                final char c = chars[prevLine + i];
                if (c == '\n') {
                    newline = i;
                    break;
                }
                if (Character.isWhitespace(c) || c == '-') {
                    maxspace = Math.max(prevLine + i, maxspace);
                }
            }

            // handle newlines:
            if (newline >= prevLine) {
                output.add(new String(chars, prevLine, newline));
                prevLine = newline + 1;
                continue;
            }

            int guess = prevLine + width;
            //System.out.println(prevLine+", "+guess+", "+chars.length);
            int split = -1;
            if (guess >= chars.length) { // end of string.
                split = chars.length;
            } else {
                split = maxspace;
            }

            Character hyphen = null;
            if (split == -1 && chars[split + 1] != ' ') {
                hyphen = '-';
                split = guess - 1;
            }
            StringBuilder sb = new StringBuilder();
            sb.append(chars, prevLine, split - prevLine);
            if (hyphen != null) {
                sb.append(hyphen);
                prevLine = split;
            } else {
                prevLine = split + 1;
            }

            output.add(sb.toString());
            if (split == chars.length)
                break;
        }
        return output;
    }

    public static boolean isWhitespace(String text) {
        for (int i = 0; i < text.length(); i++) {
            if (!Character.isWhitespace(text.charAt(i)))
                return false;
        }
        return true;
    }
}

Related

  1. getWordWrappedText(int maxWidth, String... lines)
  2. getWrappedLine(String line, int lineLength)
  3. lineWrap(String text, int width, boolean shiftNewLines)
  4. wordWrap(final String rawString, final int lineLength)
  5. wordWrap(String entry)
  6. wrapString(String str)
  7. wrapString(String text, int maxLength)
  8. wrapText(String description)
  9. wrapText(String inString, String newline, int wrapColumn)