---
title: "java实现页面字符串换行空格方法（如果原始字符串存在换行和空格）"
author: "退役的龙弟弟"
date: "2014-09-28T14:23:03Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2014-09-28T14:23:03Z"
id: 2123385
url: "https://tydldd.iteye.com/blog/2123385"
---

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