Java String Break breakString(String remainingStr, String soFar, int maxLineLength)

Here you can find the source of breakString(String remainingStr, String soFar, int maxLineLength)

Description

Creates html representation of a given string that includes line breaks

License

Apache License

Declaration

public static String breakString(String remainingStr, String soFar, int maxLineLength) 

Method Source Code

//package com.java2s;
/*/*  w  w  w.  j ava 2 s  .c om*/
 *    Copyright 2011-2012 University of Toronto
 *
 *    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 {
    /**
     * Creates html representation of a given string that includes line breaks
     */
    public static String breakString(String remainingStr, String soFar, int maxLineLength) {
        if (remainingStr.length() < maxLineLength) {
            return "<html>" + soFar + remainingStr + "</html>";
        }
        int endIndex;
        if (maxLineLength > remainingStr.length()) {
            endIndex = remainingStr.length();
        } else {
            endIndex = maxLineLength;
        }
        int indexOfBreak = remainingStr.substring(0, endIndex).lastIndexOf(" ");

        if (indexOfBreak == -1) {
            return breakString(remainingStr.substring(maxLineLength),
                    soFar += remainingStr.substring(0, maxLineLength) + "<br>-", maxLineLength);
        } else {
            soFar += remainingStr.substring(0, indexOfBreak) + "<br>";
            return breakString(remainingStr.substring(indexOfBreak), soFar, maxLineLength);
        }
    }
}

Related

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