Convert an HTML text into a ASCII text by removing HTML tags. - Java java.lang

Java examples for java.lang:String HTML

Introduction

The following code shows how to Convert an HTML text into a ASCII text by removing HTML tags.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String htmlText = "<p>java2s.com</p>";
        System.out.println(removeHtmlTags(htmlText));
    }//w  w w .  j a  v a  2  s.c  om

    /**
     * <p>Convert an HTML text into a ASCII text by removing HTML tags.
     * @param htmlText original HTML content
     * @return ASCII formatted text
     */
    public static String removeHtmlTags(final String htmlText) {
        String content = htmlText;

        int startTagIndex = content.indexOf("<");
        if (startTagIndex != -1) {
            int endTagIndex = -1;
            StringBuilder buf = new StringBuilder();

            while (true) {
                endTagIndex = content.indexOf(">");
                if (endTagIndex == -1) {
                    break;
                }
                content = content.substring(endTagIndex + 1);
                startTagIndex = content.indexOf("<");
                if (startTagIndex == -1) {
                    break;
                }
                buf.append(content.substring(0, startTagIndex));
            }
            content = buf.toString();
        }

        return content;
    }
}

Related Tutorials