Java Word Wrap wrapText(String string, int theWrapLength)

Here you can find the source of wrapText(String string, int theWrapLength)

Description

wrap Text

License

Open Source License

Declaration

public static String wrapText(String string, int theWrapLength) 

Method Source Code

//package com.java2s;
/*/*  ww w  .ja  v  a2 s .co  m*/
 * Werkzeug
 *
 * Copyright (C) 2012 Patrick Kochlik + Dennis Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * {@link http://www.gnu.org/licenses/lgpl.html}
 *
 */

import java.text.BreakIterator;

public class Main {
    public static String wrapText(String string, int theWrapLength) {
        return wrapText(string, "\n", theWrapLength);
    }

    public static String wrapText(String theString, String theLineSeparator, int theWrapLength) {
        final int myStringLength = theString.length();

        if (myStringLength > theWrapLength) {
            StringBuffer myBuffer = new StringBuffer(
                    myStringLength + ((myStringLength / theWrapLength) * 2 * theLineSeparator.length()));
            BreakIterator myLineIterator = BreakIterator.getLineInstance();
            myLineIterator.setText(theString);
            int myStart = myLineIterator.first();
            int myLineStart = myStart;

            for (int myEnd = myLineIterator
                    .next(); myEnd != BreakIterator.DONE; myStart = myEnd, myEnd = myLineIterator.next()) {
                if (myEnd - myLineStart < theWrapLength) {
                    myBuffer.append(theString.substring(myStart, myEnd));
                } else {
                    if (true || myEnd - myStart < theWrapLength) {
                        myBuffer.append(theLineSeparator);
                        myBuffer.append(theString.substring(myStart, myEnd));
                    }
                    myLineStart = myEnd;
                }
            }
            theString = myBuffer.toString();
        }

        return theString;
    }
}

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)
  5. wrapText(String s, int width, boolean noIterator)