Java String Wrap wrapText(String text, int len, boolean wordWrap)

Here you can find the source of wrapText(String text, int len, boolean wordWrap)

Description

It formats a long sentence to wrap into a desired width

License

Apache License

Parameter

Parameter Description
text The text to be wrapped
len the character width size to wrap into
wordWrap true for enabling word-wrap

Return

array of String containing the wrapped text

Declaration

public static String[] wrapText(String text, int len, boolean wordWrap) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    /**//from  w  w w .  j a v  a 2s.  c  o m
     * It formats a long sentence to wrap into a desired width
     * 
     * @param text      The text to be wrapped
     * @param len       the character width size to wrap into
     * @param wordWrap  true for enabling word-wrap
     * @return          array of String containing the wrapped text
     */
    public static String[] wrapText(String text, int len, boolean wordWrap) {
        // return empty array for null text
        if (text == null)
            return new String[] {};

        // return text if len is zero or less
        if (len <= 0)
            return new String[] { text };

        // return text if less than length
        if (text.length() <= len)
            return new String[] { text };

        char[] chars = text.toCharArray();
        ArrayList<String> lines = new ArrayList<String>();
        StringBuilder line = new StringBuilder();
        StringBuffer word = new StringBuffer();

        for (int i = 0; i < chars.length; i++) {
            word.append(chars[i]);

            if (wordWrap && chars[i] == ' ') {
                if ((line.length() + word.length()) > len) {
                    lines.add(line.toString());
                    line.delete(0, line.length());
                }
                line.append(word);
                word.delete(0, word.length());
            } else if (!wordWrap) {
                if (word.length() == len) {
                    lines.add(word.toString());
                    word.delete(0, word.length());
                }
            }
        }

        // handle any extra chars in current word
        if (word.length() > 0) {
            if ((line.length() + word.length()) > len) {
                lines.add(line.toString());
                line.delete(0, line.length());
            }
            line.append(word);
        }
        // handle extra line
        if (line.length() > 0) {
            lines.add(line.toString());
        }

        String[] ret = new String[lines.size()];
        for (int i = 0; i < lines.size(); i++) {
            ret[i] = (String) lines.get(i);
        }
        return ret;
    }
}

Related

  1. wrapString(String text, int maxLength)
  2. wrapText(String description)
  3. wrapText(String inString, String newline, int wrapColumn)
  4. wrapText(String line, int maxLineLength)
  5. wrapText(String text, int len)
  6. wrapText(String txt, char breakChar, String lineDelim, int numChars)