日期:2014-05-18  浏览次数:20755 次

自动换行的问题
数据库中的数据显示到页面上,不能自动换行
我用了
                                    content   =   content.replaceAll( "&lt; ", " < ");
                content   =   content.replaceAll( "&rt; ", "> ");

和                                 pexp=pexp.replaceAll( "\n ", " <br> ")
都不行

content和pexp是string类型字段.


------解决方案--------------------
给你一个方法,数据显示之前替换成HTML里的转意字符就OK了
(在保存到数据库之前替换也行的)
=============================================
内容: 将特殊字符转换成HTML格式
作者:LeoYUN
//Class toHTML
//CopyRight:Writed By Yun Leo(php4er@sohu.com)
//Last modify time:2003-8-31
//Method: public String toHTML(String old_string)
class toHTML
{
public String toHTML(String sStr)
{
if (sStr == null||sStr.equals( " "))
{
return sStr;
}

String sTmp = new String();
int i = 0;
while (i <= sStr.length()-1)
{
if (sStr.charAt(i) == '\n ')
{
sTmp = sTmp.concat( "
");
}
else if(sStr.charAt(i) == ' ')
{
sTmp = sTmp.concat( "%26amp;nbsp; ");
}
else if(sStr.charAt(i) == ' < ')
{
sTmp = sTmp.concat( " < ");
}
else if(sStr.charAt(i) == '> ')
{
sTmp = sTmp.concat( "> ");
}
else if(sStr.charAt(i) == 34) //34为双引号
{
sTmp = sTmp.concat( " " ");
}
else if(sStr.charAt(i) == 39) //若为单引号则用两个单引号代替
{
sTmp = sTmp.concat( " ' ' ");
}
else
{
sTmp = sTmp.concat(sStr.substring(i,i+1));
}
i++;
}
return sTmp;
}
}

------解决方案--------------------
看下面这2个转换吧:
//转换成Html格式
public String GetHtmlEncode(String StrValue) {
String str=StrValue;
str = str.replace( "& ", "&amp; ");
str = str.replace( " ' ", " ' ' ");
str = str.replace( "\ " ", "&quot; ");
str = str.replace( " ", "&nbsp; ");
str = str.replace( " < ", "&lt; ");
str = str.replace( "> ", "&gt; ");
str = str.replace( "\n ", " <br/> ");
return str;
}
//转换成textarea格式
public String ReadHtmlEncode(String StringValue) {
String str=StringValue;
str = str.replace( " <br/> ", "\n ");
str = str.replace( "&gt; ", "> ");
str = str.replace( "&lt; ", " < ");
str = str.replace( "&nbsp; ", " ");
str = str.replace( "&quot; ", "\ " ");
str = str.replace( " ' ' ", " ' ");
str = str.replace( "&amp; ", "& ");
return str;
}
------解决方案--------------------
mark
------解决方案--------------------
jsp上直接用 <%%> 来显数据,你的数据在要换行的地方改成 <BR>