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

本文详细介绍了Java中用于处理字符串的工具类,包括去除空格、去除HTML标签等功能的实现方法,通过具体代码示例展示了如何操作和优化字符串处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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&amp;imageType=k&amp;token=28dfeb41d35cc81d84b4664d7c23c49f&amp;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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值