Java String Strip stripTags(String html)

Here you can find the source of stripTags(String html)

Description

Remove all text contained within "< >" tags.

License

Open Source License

Parameter

Parameter Description
html The source html string.

Return

The raw text.

Declaration

public static String stripTags(String html) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from  w ww . j a v a2 s. c om*/
     * Remove all text contained within "&lt; &gt;" tags.
     *
     * @param html The source html string.
     * @return The raw text.
     */
    public static String stripTags(String html) {
        StringBuilder stripped = new StringBuilder();
        while (html.length() > 0) {
            int idx = html.indexOf("<");
            if (idx < 0) {
                stripped.append(html.trim());
                break;
            }
            String text = html.substring(0, idx);
            text = text.trim();
            if (text.length() > 0) {
                stripped.append(text + " \n");
            }
            html = html.substring(idx);
            int idx2 = html.indexOf(">");
            if (idx2 < 0) {
                break;
            }
            html = html.substring(idx2 + 1);
        }

        stripped = new StringBuilder(replace(stripped.toString(), "&nbsp;",
                ""));
        return stripped.toString();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1,
            Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }

    /**
     * Replaces all occurrences of "pattern" in "string" with "value"
     *
     * @param string  string to munge
     * @param pattern pattern to replace
     * @param value   replacement value
     * @return munged string
     */
    private static String replace(String string, String pattern,
            String value) {
        if (pattern.length() == 0)
            return string;

        StringBuilder returnValue = new StringBuilder();
        int patternLength = pattern.length();
        while (true) {
            int idx = string.indexOf(pattern);
            if (idx < 0) {
                break;
            }
            returnValue.append(string.substring(0, idx));
            if (value != null) {
                returnValue.append(value);
            }
            string = string.substring(idx + patternLength);
        }
        returnValue.append(string);
        return returnValue.toString();
    }

    /**
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuilder buf = new StringBuilder();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }
}

Related

  1. stripExtension(String filename)
  2. stripLastElement(String str)
  3. stripLeadingSlash(String str)
  4. stripNamespaceDeclarations(String xml)
  5. stripTags(final String tagged)
  6. stripTags(String in)
  7. stripTrailingChar(String input, char c)
  8. stripUnsupportedChars(String str)