Java Word Wrap wrapText(String s, int width, boolean noIterator)

Here you can find the source of wrapText(String s, int width, boolean noIterator)

Description

wrap Text

License

Open Source License

Declaration

public static String wrapText(String s, int width, boolean noIterator) 

Method Source Code

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

import java.text.BreakIterator;

import java.util.StringTokenizer;

public class Main {
    private static final int DEFAULT_WIDTH = 40;

    /**//from  w w  w. j  a  v a 2s. c  o m
     * Utility function to wrap text at given width. Used mostly
     * for displaying text in the dialog box.
     *
     * @param s message string
     * @param width width per line
     * @return string with line feeds
     */
    public static String wrapText(String str, int width) {
        if (str == null || str.length() < width) {
            return str;
        }
        String ret = "";

        StringTokenizer tokenizer = new StringTokenizer(str, "\n");
        while (tokenizer.hasMoreTokens()) {
            BreakIterator boundary = BreakIterator.getLineInstance();
            String s = tokenizer.nextToken();
            boundary.setText(s);
            int end;
            int start = 0;
            while ((end = boundary.next()) != BreakIterator.DONE) {
                if (end - start > width) {
                    end = boundary.previous();
                    if (start == end) {
                        // Was too long for even one iteration
                        end = boundary.next();
                    }
                    ret += s.substring(start, end);
                    ret += "\n";
                    start = end;
                }
            }
            end = boundary.last();
            ret = ret + s.substring(start, end) + "\n";
        }
        return ret;
    }

    public static String wrapText(String s, int width, boolean noIterator) {
        if (noIterator) {
            if (s == null || s.length() <= width) {
                return s;
            }

            String ret = "";
            int start = 0;
            int end = width;
            int len = s.length();
            while (len > width) {
                ret += s.substring(start, end);
                ret += "\n";
                len -= width;
                start += width;
                end += width;
            }
            ret += s.substring(start);
            return ret;
        } else {
            return wrapText(s, width);
        }
    }

    /**
     * Utility function to wrap text at default width. Used mostly
     * for displaying text in the dialog box.
     *
     * @param s message string
     * @return string with line feeds
     */
    public static String wrapText(String s) {
        return (wrapText(s, DEFAULT_WIDTH));
    }
}

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. wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
  6. wrapText(String string, int theWrapLength)