Android HTML String Escape stripHTMLTags(String textToStrip)

Here you can find the source of stripHTMLTags(String textToStrip)

Description

Strip a String by removing HTML elements

License

Apache License

Parameter

Parameter Description
textToStrip String to strip

Return

stripped

Declaration

public static String stripHTMLTags(String textToStrip) 

Method Source Code

//package com.java2s;
/*//from ww w .  j  av a 2s  .  c  o  m
 * (C) Copyright Itude Mobile B.V., The Netherlands
 * 
 * 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.
 */

import java.text.CharacterIterator;

import java.text.StringCharacterIterator;

public class Main {
    /**
     * Strip a {@link String} by removing HTML elements
     * 
     * @param textToStrip {@link String} to strip
     * @return stripped {@link String}
     */
    public static String stripHTMLTags(String textToStrip) {

        StringBuffer returnText = new StringBuffer(textToStrip.length());

        CharacterIterator iterator = new StringCharacterIterator(
                textToStrip);

        boolean finished = true;
        boolean started = false;
        for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator
                .next()) {
            if (ch == '<') {
                started = true;
            } else if (ch == '>') {
                started = false;
                finished = true;
            } else if (finished && !started) {
                returnText.append(ch);
            }

        }

        return returnText.toString().trim();
    }
}

Related

  1. escapeHtml(String html)
  2. checkHtmlView(String strString)
  3. toHTMLString(String s)
  4. isHTML(String s)