Java String Split by Length splitHtmlString(String stringToSplit, int maxLength)

Here you can find the source of splitHtmlString(String stringToSplit, int maxLength)

Description

Split a line of text in lines no longer than maxLength characters.

License

Open Source License

Parameter

Parameter Description
stringToSplit the line of text to split
maxLength the maximum length of each resulting line of text

Return

A List of strings representing the split text

Declaration

public static List<String> splitHtmlString(String stringToSplit, int maxLength) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .  ja v a  2 s . c o m*/
 * Geotag
 * Copyright (C) 2007-2016 Andreas Schneider
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Split a line of text in lines no longer than maxLength characters. this
     * routine assumes that there are no words longer than maxLength.
     * 
     * @param stringToSplit
     *          the line of text to split
     * @param maxLength
     *          the maximum length of each resulting line of text
     * @return A List of strings representing the split text
     */
    public static List<String> splitHtmlString(String stringToSplit, int maxLength) {
        List<String> lines = new ArrayList<String>();
        boolean processingHtmlTag = false;
        StringBuilder word = new StringBuilder();
        StringBuilder line = new StringBuilder();
        for (int index = 0; index < stringToSplit.length(); index++) {
            char character = stringToSplit.charAt(index);
            if (processingHtmlTag) {
                // always append - doesn't add to length
                word.append(character);
                switch (character) {
                case '>':
                    processingHtmlTag = false;
                    break;
                default:
                }
            } else {
                switch (character) {
                case '<':
                    word.append(character);
                    processingHtmlTag = true;
                    break;
                case ' ':
                    if (line.length() + word.length() + 1 < maxLength) {
                        line.append(word.toString()).append(' ');
                        word = new StringBuilder();
                    } else {
                        lines.add(line.toString());
                        line = new StringBuilder();
                        word.append(' ');
                    }
                    break;
                default:
                    word.append(character);
                }
            }
        }
        if (word.length() > 0) {
            line.append(word.toString());
        }
        if (line.length() > 0) {
            lines.add(line.toString());
        }
        return lines;
    }
}

Related

  1. splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)
  2. splitByWholeSeparatorWorker(final String str, final String separator, final int max, final boolean preserveAllTokens)
  3. splitEqually(String _text, int _len)
  4. splitEscapedString(String str, char sep, char esc, int maxPart)
  5. splitFilenames(String text)
  6. splitIntoFrames(String src, int maxFrameSize)
  7. splitIntoLine(String input, int maxCharInLine)
  8. splitLines(String paragraph, int lineLength)
  9. splitParams(String string, int max)