Java中替换HTML标签的方法代码

这篇文章主要介绍了Java中替换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、运行结果如下

打印替换后的字符串:     
    序号  

    李四  

    赵六 

posted @ 2014-09-04 17:59  GisClub  阅读(3046)  评论(0编辑  收藏  举报