Java String remove HTML tags

Description

Java String remove HTML tags

//package com.demo2s;

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String string = "<b>demo2s.com</b>";
        System.out.println(stripHtmlTags(string));
    }// ww  w  .  j  a v  a2 s . c  o m

    private static final Pattern htmlTagPattern = Pattern.compile("</?[a-zA-Z][^>]*>");

    /**
     * Given a <code>String</code>, returns an equivalent <code>String</code> with
     * all HTML tags stripped. Note that HTML entities, such as "&amp;amp;" will
     * still be preserved.
     */
    public static String stripHtmlTags(String string) {
        if ((string == null) || "".equals(string)) {
            return string;
        }
        return htmlTagPattern.matcher(string).replaceAll("");
    }

    
}



PreviousNext

Related