---
title: "从数据库中读取带换行的字符串"
author: "pphqq"
date: "2011-06-01T09:25:04Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-06-01T09:25:04Z"
id: 1066872
url: "https://pphqq.iteye.com/blog/1066872"
---

数据库中的workExp（工作经历）字段是带换行或回车的字符串。再取出拼json的时候，换行会破坏json格式。于是先将该字符串中的换行或是回车替换成“；”，将替换好的字符串拼入json字符串中，传入前台再将“；”换成换行符即可。主要代码如下：

```java
后台：
			//workexp 工作经历中包含换行，进行处理后放入json字符串中。
			BufferedReader br = new BufferedReader(new StringReader(user.getWorkExp().trim()));
			String sting ="";
			try
			{
			   String line =  br.readLine();
				while(line != null){
					sting += line+';';
				    line = br.readLine(); 
				   }
			   br.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
前台：
var workexp = node.attributes.workExp.replace(/;/g,"\n");
parent.document.getElementById("WorkExp").value=workexp;

```