public class Test{
public static String Replace(
String source,
String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for (posStart = 0;
(pos = source.indexOf(oldString, posStart)) >= 0;
posStart = pos + lengOfold) {
output.append(source.substring(posStart, pos));
output.append(newString);
} if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
public static String HtmlWrite(String s) {
s = Replace(s, "&", "&;");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
return s;
} //测试,将s 换成,你数据库中读出的CLOB即可
public static void main(String args[]) {
String s = HtmlWrite("asdfasdf\t\nsdfasdf\nadfaf");
System.out.println(s);
}}
public static String Replace(
String source,
String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for (posStart = 0;
(pos = source.indexOf(oldString, posStart)) >= 0;
posStart = pos + lengOfold) {
output.append(source.substring(posStart, pos));
output.append(newString);
} if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
public static String HtmlWrite(String s) {
s = Replace(s, "&", "&;");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
return s;
} //测试,将s 换成,你数据库中读出的CLOB即可
public static void main(String args[]) {
String s = HtmlWrite("asdfasdf\t\nsdfasdf\nadfaf");
System.out.println(s);
}}
解决方案 »
- jsp URL传参问题
- JSP游标出错??
- jsp显示小数问题?
- hibernate从oracle中取出数据来,String类型的,但是长度跟输入的不一样
- 谁有用java开发的网站源代码
- 帮帮忙,一个jdbc与splserver2k的连接问题
- 在weblogic下 使用fileupload抛出NoSuchMethodException
- 请问想生成一个myapp目录,并通过http://localhost:8080/myapp来访问,应该怎么做?
- 请问在jsp中记录的添加的问题
- 关于FORM提交数据的问题,若能解决我给你200分!
- Easy problem:在<%! %> 中定义的变量和在<% %> 定义的变量到底有哪些不同?
- 我做的第一个BEAN编译过了,可是不能执行大家帮忙侃侃。谢谢。
方法
s = Replace(s, "&", "&;");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
这样的转化
第一种:public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer();
for (int i = 0; in != null && i < in.length(); i++) {
char c = in.charAt(i);
if (c == '\'')
out.append("'");
else if (c == '\"')
out.append(""");
else if (c == '<')
out.append("<");
else if (c == '>')
out.append(">");
else if (c == '&')
out.append("&");
else if (c == ' ')
out.append(" ");
else if (c == '\n')
out.append("<br/>");
else
out.append(c);
}
return out.toString();
}第二种:
public String encode(String s)
{
if(s==null)return null;
StringBuffer sb = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
switch (c) {
case ' ': sb.append(" ");break;
case '\r':sb.append("<br/>");break;
case '\'':sb.append("'");break;
case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '&': sb.append("&"); break;
case '"': sb.append("""); break;
default: sb.append(c);
}
}
return sb.toString();
}