ITeye博客

java实现页面字符串换行空格方法(如果原始字符串存在换行和空格)

退役的龙弟弟 · 2014-09-28 14:23 · javaweb · 编程语言 · Markdown

适用于xml、json以及分条字符串

 

/**
	 * 字符串格式化处理(实现字符串在回车换行和空格功能)
	 * @param xml
	 */
	private String convertstr(String xmlorjson) {
		if (xmlorjson == null || "".equals(xmlorjson) || "null".equals(xmlorjson)) {
			return "";
		}
		//处理字符串
		String[] beforechar = {"<", ">", "¢", "£", "¥", "€", "§", "©", "®", "™", "×", "÷"};
		String[] afterchar = {"&lt;", "&gt;", "&cent;", "&pound;", "&yen;", 
				"&euro;", "&sect;", "&copy;", "&reg;", "&trade;", "&times;", "&divide;"};
		for (int i = 0; i < beforechar.length; i++) {
			xmlorjson = xmlorjson.replaceAll(beforechar[i], afterchar[i]);
		}
		xmlorjson = xmlorjson.replaceAll("\n", "</br>");
		xmlorjson = xmlorjson.replaceAll(" ", "&nbsp;");
		xmlorjson = xmlorjson.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
		return xmlorjson;
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。