要求:去掉字符串中的所有HTML标签。
同事提供的方法:
利用别人写好的程序实现,引入第三方jar包:jericho-html-3.2.jar
工具类:
/**
* 去掉html标签,得到纯文本内容
* @param content 要处理的源,带html标签
* @return
*/
public static String getPlainTextWithoutHtmlTag(String content) {
if(content == null || "".equals(content)){
return "";
}else{
Source src = new Source(content);
String str = src.getTextExtractor().toString().trim();
return str.replaceAll(" ","").replaceAll(" ", "");
}
}
测试:
public static void clearFieldChiNameHtmlTag()
{
String str = "<b>一、主营业务收入 </b>";
System.out.println(StringUtil.getPlainTextWithoutHtmlTag(str));
}
测试结果:
一、主营业务收入