`
退役的龙弟弟
  • 浏览: 446324 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

适用于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;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics