Java String Break breakString(String theString)

Here you can find the source of breakString(String theString)

Description

Breaks the String theString at position #CONSOLE_WIDTH and adds a linebreak.

License

Apache License

Parameter

Parameter Description
theString String to be break

Return

the breaked String

Declaration

public static String breakString(String theString) 

Method Source Code

//package com.java2s;
/**//from  ww  w. ja v  a  2  s.  c  om
 * Copyright 2009 Humboldt-Universit?t zu Berlin, INRIA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 */

public class Main {
    /**
     * The standard width of the output console of Pepper.
     */
    public final static int CONSOLE_WIDTH = 120;

    /**
     * Breaks the String <code>theString</code> at position
     * {@link #CONSOLE_WIDTH} and adds a linebreak. The manipulated String is
     * returned.
     * 
     * @param theString
     *            String to be break
     * @return the breaked String
     */
    public static String breakString(String theString) {
        return (breakString("", theString, CONSOLE_WIDTH));
    }

    /**
     * Breaks the String <code>theString</code> at position <code>length</code>
     * and adds a linebreak. The manipulated String is returned.
     * 
     * @param theString
     *            String to be break
     * @param length
     *            position where to break String
     * @return the breaked String
     */
    public static String breakString(String theString, int length) {
        return (breakString("", theString, length));
    }

    /**
     * Breaks the String <code>theString</code> at position
     * {@link #CONSOLE_WIDTH} and adds a linebreak. The next line than is
     * prefixed by<code>linePrefix</code>. The manipulated String is returned.
     * 
     * @param theString
     *            String to be break
     * @param length
     *            position where to break String
     * @param linePrefix
     *            a prefix for all lines
     * @return the breaked String
     */
    public static String breakString(String linePrefix, String theString) {
        return (breakString(linePrefix, theString, CONSOLE_WIDTH));
    }

    /**
     * Breaks the String <code>theString</code> at position <code>length</code>
     * and adds a linebreak. The next line than is prefixed by
     * <code>linePrefix</code>. The manipulated String is returned.
     * 
     * @param theString
     *            String to be break
     * @param length
     *            position where to break String
     * @param linePrefix
     *            a prefix for all lines
     * @return the breaked String
     */
    public static String breakString(String linePrefix, String theString,
            int length) {
        if (length > theString.length() + linePrefix.length()) {
            return (theString);
        }
        StringBuilder str = new StringBuilder();
        int pos = 0;
        int offset = length - linePrefix.length();
        boolean goOn = true;
        while (goOn) {
            str.append(linePrefix);
            if (pos + offset < theString.length()) {
                str.append(theString.substring(pos, pos + offset));
                str.append("\n");
            } else {
                str.append(theString.substring(pos));
                goOn = false;
            }
            pos = pos + offset;
        }
        return (str.toString());
    }
}

Related

  1. breakString(String input, int partLength)
  2. breakString(String remainingStr, String soFar, int maxLineLength)
  3. breakString(String str, int maxLineLenght)
  4. breakString(String string, int l, String newLine)
  5. breakString(String text, int maxsize)