Tuesday, September 21, 2010

How to remove HTML Tags from Java String

1. You can try like this to remove html tags from string

String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");
System.out.println(noHTMLString);

2. Another complicated example

public static String removeHTML(String htmlString)
{
// Remove HTML tag from java String
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

// Remove Carriage return from java String
noHTMLString = noHTMLString.replaceAll("\r", "
");

// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\'", "'");
noHTMLString = noHTMLString.replaceAll("\"", """);
return noHTMLString;
}

1 comment:

  1. please explain meaning of regular expression useed

    ReplyDelete