Java String Split splitHTMLTags(final String string)

Here you can find the source of splitHTMLTags(final String string)

Description

Splits the given string on spaces and certain embedded HTML tags.

License

Open Source License

Parameter

Parameter Description
string the given string

Return

the words and HTML tags that compose the string

Declaration

public static List<String> splitHTMLTags(final String string) 

Method Source Code

//package com.java2s;
/*/*from  w ww.j  a v  a 2s  . c om*/
 * StringUtils.java
 *
 * Created on October 4, 2006, 2:36 PM
 *
 * Description:
 *
 * Copyright (C) 2006 Stephen L. Reed.
 *
 * 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, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /** Splits the given string on spaces and certain embedded HTML tags.
     *
     * @param string the given string
     * @return the words and HTML tags that compose the string
     */
    public static List<String> splitHTMLTags(final String string) {
        if (!string.contains("<")) {
            return splitOnSpace(string);
        }
        String tempString = string;
        tempString = replace(tempString, "<br>", " <br> ");
        tempString = replace(tempString, "<ol>", " <ol> ");
        tempString = replace(tempString, "</ol>", " </ol> ");
        tempString = replace(tempString, "<ul>", " <ul> ");
        tempString = replace(tempString, "</ul>", " </ul> ");
        tempString = replace(tempString, "<li>", " <li> ");
        tempString = replace(tempString, "</li>", " </li> ");
        tempString = replace(tempString, "<strong>", " <strong> ");
        tempString = replace(tempString, "</strong>", " </strong> ");
        return splitOnSpace(tempString.trim());
    }

    /** Splits the given string on spaces, which is faster than String.split(...) for this special case.
     *
     * @param string the given string
     * @return the words that compose the string
     */
    public static List<String> splitOnSpace(final String string) {
        final List<String> words = new ArrayList<>();
        final int string_len = string.length();
        int index = 0;
        for (int i = 0; i < string_len; i++) {
            final char ch = string.charAt(i);
            if (ch == ' ') {
                if (i > index) {
                    words.add(string.substring(index, i));
                }
                index = i + 1;
            }
        }
        if (index < string_len) {
            words.add(string.substring(index));
        }
        return words;
    }

    /** Replaces all occurrences of a substring within a string with
     * another string.
     * @param inString String to examine
     * @param oldPattern String to replace
     * @param newPattern String to insert
     * @return a String with the replacements
     */
    public static String replace(final String inString, final String oldPattern, final String newPattern) {
        //Preconditions
        assert inString != null : "inString must not be null";
        assert oldPattern != null : "oldPattern must not be null";
        assert newPattern != null : "newPattern must not be null";

        if (!hasLength(inString) || !hasLength(oldPattern)) {
            return inString;
        }
        final StringBuilder stringBuilder = new StringBuilder();
        int pos = 0; // our position in the old string
        int index = inString.indexOf(oldPattern);
        // the index of an occurrence we've found, or -1
        final int patLen = oldPattern.length();
        while (index >= 0) {
            stringBuilder.append(inString.substring(pos, index));
            stringBuilder.append(newPattern);
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
        }
        stringBuilder.append(inString.substring(pos));
        // remember to append any characters to the right of a match
        return stringBuilder.toString();
    }

    /** Returns whether the given CharSequence is neither <code>null</code> nor length 0.
     * Note: Will return <code>true</code> for a CharSequence that purely consists of whitespace.
     * <p><pre>
     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * </pre>
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return <code>true</code> if the CharSequence is not null and has length
     * @see #hasText(String)
     */
    public static boolean hasLength(final CharSequence str) {
        return (str != null && str.length() > 0);
    }

    /** Returns whether the given String is neither <code>null</code> nor length 0.
     * Note: Will return <code>true</code> for a String that purely consists of whitespace.
     * @param str the String to check (may be <code>null</code>)
     * @return <code>true</code> if the String is not null and has length
     * @see #hasLength(CharSequence)
     */
    public static boolean hasLength(final String str) {
        return hasLength((CharSequence) str);
    }
}

Related

  1. splitFields(String fieldsString, int minNum)
  2. splitForIndexMatching(String string)
  3. splitGenericIfNeeded(String name)
  4. splitHelperName(String name)
  5. splitHistory(String history)
  6. splitIdKeyConfig(String config)
  7. splitInitialItems(final String text)
  8. splitInput(String cliInput)
  9. splitIntArray(final String iInput)