Search This Blog

Monday, August 30, 2010

How to remove HTML tags from a Java String?

Some times we need to remove HTML tags from a java string before publishing it to the page. We can achieve this by using a regular express. Sample code is given below for your reference.

public class RemoveTags{
    public static void main(String a[]){
        String text = "<b>I dont want this to be bold<\b>";
        text = text.replaceAll("\\<.*?\\>", "");
        System.out.println(text);
    }
}

Output:

I dont want this to be bold

How to remove non-ASCII characters from a Java String?

We can remove non-ASCII characters from a Java String by using below regular expression.

String.replaceAll("[^\\p{ASCII}]", ""))