Java中替换HTML标签的方法

1、替换HTML标签

replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "\n\t")

源码如下:
/**
 * 字符串替换
 */
package com.you.model;

/**
 * HTML标签替换
 * @author YHD
 * @version
 */
public class StrReplace {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		String str = " <table border='1' cellpadding='1' cellspacing='1'><tr><th rowspan='2'>序号</th><th colspan='2'>李四</th><th>赵六</th></tr><table>";
		/**
		 * 替换HTML标签
		 */
		String subStr = str.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "\n\t");
		/**
		 * 打印替换后的字符串
		 */
		System.out.println("打印替换后的字符串:" + subStr);
	}

}

2、运行结果如下
打印替换后的字符串: 
	
	
	序号
	
	李四
	
	赵六


再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/odejsjhshw/p/10387695.html