Java Word Wrap wordWrap(String input, int width, Locale locale)

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

Description

Reformats a string where lines that are longer than width are split apart at the earliest wordbreak or at maxLength, whichever is sooner.

License

Apache License

Parameter

Parameter Description
input the String to reformat.
width the maximum length of any one line.

Return

a new String with reformatted as needed.

Declaration

public static String wordWrap(String input, int width, Locale locale) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j ava2s . co m
 * Copyright 2001-2013 Geert Bevin (gbevin[remove] at uwyn dot com)
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.text.BreakIterator;
import java.util.*;

public class Main {
    /**
     * Reformats a string where lines that are longer than <tt>width</tt>
     * are split apart at the earliest wordbreak or at maxLength, whichever is
     * sooner. If the width specified is less than 5 or greater than the input
     * Strings length the string will be returned as is.
     * <p/>
     * Please note that this method can be lossy - trailing spaces on wrapped
     * lines may be trimmed.
     *
     * @param input the String to reformat.
     * @param width the maximum length of any one line.
     * @return a new String with reformatted as needed.
     */
    public static String wordWrap(String input, int width, Locale locale) {
        // handle invalid input
        if (input == null) {
            return "";
        } else if (width < 5) {
            return input;
        } else if (width >= input.length()) {
            return input;
        }

        // default locale
        if (locale == null) {
            locale = Locale.US;
        }

        StringBuilder buffer = new StringBuilder(input.length());
        int current_index = 0;
        int delimiter_index = 0;
        String seperator = "\n";
        String line;

        // go over the input string and jump from line to line
        while (current_index <= input.length()) {
            // look for the next linebreak
            delimiter_index = input.indexOf(seperator, current_index);

            // get the line that corresponds to it
            if (-1 == delimiter_index) {
                line = new String(input.substring(current_index, input.length()));
                current_index = input.length() + 1;
            } else {
                line = new String(input.substring(current_index, delimiter_index));
                current_index = delimiter_index + seperator.length();
            }

            // handle the wrapping of the line
            BreakIterator breaks = BreakIterator.getLineInstance(locale);
            breaks.setText(line);

            int line_start = 0;
            int start = breaks.first();
            int end = breaks.next();
            while (end != BreakIterator.DONE) {
                // check if the width has been exceeded
                if (end - 1 - line_start >= width) {
                    boolean break_line = true;

                    // first check if the last characters were spaces,
                    // if they were and by removing them the width is not
                    // exceeded, just continue
                    if (Character.isWhitespace(line.charAt(end - 1))) {
                        for (int j = end - 1; j >= 0; j--) {
                            if (!Character.isWhitespace(line.charAt(j))) {
                                if (j - line_start < width) {
                                    break_line = false;
                                }

                                break;
                            }
                        }
                    }

                    if (break_line) {
                        String line_breaked = line.substring(line_start, start);
                        // this can happen with trailing whitespace
                        if (line_breaked.length() > width) {
                            line_breaked = line_breaked.substring(0, width);
                        }
                        buffer.append(line_breaked);

                        buffer.append("\n");

                        line_start = start;
                    }
                }

                start = end;
                end = breaks.next();
            }

            if (line_start < line.length()) {
                buffer.append(line.substring(line_start));
            }

            if (delimiter_index != -1) {
                buffer.append("\n");
            }
        }

        return buffer.toString();
    }
}

Related

  1. wordWrap(String input, int width, Locale locale)
  2. wordWrap(String input, int width, Locale locale)
  3. wrapString(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
  4. wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)