public class StringTool {
/**
* 去除 字符串中 所包含的空格
*
* <pre>
* 包括:空格(全角,半角)、制表符、换页符等
* </pre>
*
* @param xhtml
* @return
*/
public static String removeAllBlank(String xhtml) {
String result = "";
if (null != xhtml && !"".equals(xhtml)) {
result = xhtml.replaceAll("[ *| *| *|//s*]*", "");
}
return result;
}
/**
* 去除字符串中 头部 和 尾部 所包含的空格
*
* <pre>
* 包括:空格(全角,半角)、制表符、换页符等
* </pre>
*
* @param xhtml
* @return
*/
public static String trim(String xhtml) {
String result = "";
if (null != xhtml && !"".equals(xhtml)) {
result = xhtml.replaceAll("^[ *| *| *|//s*]*", "").replaceAll("[ *| *| *|//s*]*$", "");
}
return result;
}
/**
* 删除html标签
*
* @param xhtml
* @return
*/
public static String removeHtml(String xhtml) {
String result = "";
if (null != xhtml && !"".equals(xhtml)) {
result = xhtml.replaceAll("<[^>]*>|\\s+", "");
}
return result;
}
public static void main(String[] args) {
String xhtml = "\n <p style=\"display:none;height:1px;overflow:hidden;\"> </p>\n \n <p style=\"text-align:center;\"><img src=\"https://pifm.eastmoney.com/EM_Finance2014PictureInterface/Index.aspx?id=0020432&imageType=k&token=28dfeb41d35cc81d84b4664d7c23c49f&at=1\" border=\"0\" alt=\"K图 002043_2\" data-code=\"K 002043|0|2\" style=\"border:#d1d1d1 1px solid;\" width=\"578\" height=\"276\"></p>\n \n <p> 兔宝宝(002043)1月3日晚公告,截至1月3日,公司累计回购2419万股,占公司总股本的3%,成交最高价为6.81元/股,成交最低价为4.98元/股,支付的总金额为1.34亿元。 <br></p>\n <p >(文章来源:证券时报网)</p>\n";
xhtml = removeHtml(xhtml);
System.out.println(xhtml.trim());
xhtml = trim(xhtml);
System.out.println(xhtml.trim());
}
}
转载https://blog.csdn.net/Yan456jie/article/details/62056848