替换table为指定字符串

	public static void main(String[] args) {
		String content = "ddddddddddd<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD>" +
							"<TD>&nbsp;</TD><TD>&nbsp;</TD></TR><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>ss&nbsp;</TD><TD>&nbsp;ss</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR></TBODY>" +
							"</TABLE></TD></TR></TBODY></TABLE></TD><TD>&nbsp;ss</TD><TD>&nbsp;ss</TD><TD>&nbsp;ss</TD></TR><TR bgColor=#ffffff><TD>&nbsp;ss</TD><TD>&nbsp;" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;ss</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;ss</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD><TD>&nbsp;ss</TD><TD>&nbsp;</TD></TR><TR bgColor=#ffffff>" +
							"<TD>&nbsp;ss</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><P>ssssssssssssssssssssssssss</P>" +
							"<P>sssssssssssssssssssssssssssssssss</P><P>" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD>" +
							"<TD>&nbsp;</TD></TR></TBODY></TABLE>sssssssssssssssssssssssssssssssssssssssssss</TD></TR></TBODY></TABLE></P>"+ 
							"ddddddddddd<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD>" +
							"<TD>&nbsp;</TD><TD>&nbsp;</TD></TR><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>ss&nbsp;</TD><TD>&nbsp;ss</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR></TBODY>" +
							"</TABLE></TD></TR></TBODY></TABLE></TD><TD>&nbsp;ss</TD><TD>&nbsp;ss</TD><TD>&nbsp;ss</TD></TR><TR bgColor=#ffffff><TD>&nbsp;ss</TD><TD>&nbsp;" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;ss</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;ss</TD></TR>" +
							"<TR bgColor=#ffffff><TD>&nbsp;</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD><TD>&nbsp;ss</TD><TD>&nbsp;</TD></TR><TR bgColor=#ffffff>" +
							"<TD>&nbsp;ss</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><P>ssssssssssssssssssssssssss</P>" +
							"<P>sssssssssssssssssssssssssssssssss</P><P>" +
							"<TABLE cellSpacing=1 cellPadding=0 width='100%' border=0><TBODY><TR><TD bgColor=#ffffff>" +
							"<TABLE cellSpacing=1 cellPadding=2 width='100%' bgColor=#e8e8e8 border=0><TBODY><TR bgColor=#ffffff><TD>&nbsp;</TD><TD>&nbsp;</TD>" +
							"<TD>&nbsp;</TD></TR></TBODY></TABLE>sssssssssssssssssssssssssssssssssssssssssss</TD></TR></TBODY></TABLE></P>";
		if (content.contains("<TABLE") && content.contains("</TABLE>")) {
			String cc = content;
			content = content.substring(content.indexOf("<TABLE"), content.lastIndexOf("</TABLE>") + 8);
			String[] str = content.split("</TABLE>");		
			List list = new ArrayList();
			int i = 0 ;
			for (int j = 0; j < str.length; j++) {
				System.out.println(str[j]);
			}
			copeTableContent(str, i, list);
			for (int m = 0; m < list.size(); m++) {
				System.out.println("--" + list.get(m));
				content = content.replaceFirst((String)list.get(m), "<TABLE ID=" + String.valueOf(m+1) + ">");
			}
			System.out.println(content);
		}
	}
	/**
	 * 计算最外层table的内容行。
	 * @param str
	 * @param i
	 * @return
	 */
	public static int copeTableNum(String[] str, int i) {
		int num = haveNumber(str[i], 0);
		int j = i + 1;
		while (j < (i + num)) {
			num +=haveNumber(str[j], 0);
			j++;
		}
		return i + num;
	}
	/**
	 * 组合table放入List集合中
	 * @param str
	 * @param i
	 * @param list
	 */
	public static void copeTableContent(String[] str, int i, List list) {
		if (i < str.length) {
			int num = copeTableNum(str, i);
			if (num <= str.length) {
				String tem = "";
				for (int k = i; k < num; k++) {
					tem += str[k];
					tem += "</TABLE>";
				}
				tem = tem.substring(tem.indexOf("<TABLE"));
				list.add(tem);
				copeTableContent(str, num, list);
			}
		}
	}
	/**
	 * 计算 "<TABLE"标签的个数
	 * @param str
	 * @param i
	 * @return
	 */
	public static int haveNumber(String str, int i) {
		if (str.contains("<TABLE")) {
			str = str.replaceFirst("<TABLE", "");
			i++;
			i = haveNumber(str, i);
		}
		return i;
	}

你可能感兴趣的:(J#)