I came upon a situation in which I needed to find out if String was "really" empty (not just spaces and junk).  The problem was that the String might have empty HTML tags, like <p></p>.  This handy function removes all the HTML tags, leaving any real content.

function removeHtmlTags(String original) {
    return original.replaceAll("\<.*?>", "");

The results?

removeHtmlTags("<p></p>")  returns the empty string, but

removeHtmlTags("<p>foo</p>")  returns "foo"

Note: This article originally appears in my Java blog (javanature.com).  I’m shutting that one down, so I’m moving the good stuff that is left over here!

One Response to “Strip HTML Tags in Java”

  1. how to remove all tag include text

Leave a Reply

Your email address will not be published. Required fields are marked *