Here you can find the source of splitHtmlString(String stringToSplit, int maxLength)
Parameter | Description |
---|---|
stringToSplit | the line of text to split |
maxLength | the maximum length of each resulting line of text |
public static List<String> splitHtmlString(String stringToSplit, int maxLength)
//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; } }