java 正则表达式 去掉 文章头部和尾部的空格(全角,半角)、制表符、换页符

public class StringTool {
     

	/**
	 * 去除 字符串中 所包含的空格
	 * 
	 * 
	 * 包括:空格(全角,半角)、制表符、换页符等
	 * 
* * @param xhtml * @return */
public static String removeAllBlank(String xhtml) { String result = ""; if (null != xhtml && !"".equals(xhtml)) { result = xhtml.replaceAll("[ *| *| *|//s*]*", ""); } return result; } /** * 去除字符串中 头部 和 尾部 所包含的空格 * *
	 * 包括:空格(全角,半角)、制表符、换页符等
	 * 
* * @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

\n \n

\"K图

\n \n

  兔宝宝(002043)1月3日晚公告,截至1月3日,公司累计回购2419万股,占公司总股本的3%,成交最高价为6.81元/股,成交最低价为4.98元/股,支付的总金额为1.34亿元。

\n

(文章来源:证券时报网)

\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

你可能感兴趣的:(Java基础,java,正则,空格,制表符)