请问如何用JSP把存在数据库中的换行和回车转成
(一定给分)

newhu 2001-11-08 12:50:27
数据库中是用ASCII码存储换行和回车的,但现在要在HTML的表格中正常显示
回车和换行,不知哪位高手知道方法,多谢
...全文
472 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcgui 2002-07-14
  • 打赏
  • 举报
回复
搞定了吧
yun15291li 2002-07-13
  • 打赏
  • 举报
回复
String demo=request.getParameter("demo");
int index=0;
while((index=demo.indexOf("\n"))!=-1)
demo=demo.substring(0,index)+"<br>"+demo.substring(index+1);
out.print(demo);

demo是你从textarea中传过来的值
wxg4988 2002-07-13
  • 打赏
  • 举报
回复
试一下
public static String returnToBr(String sStr)
{
if (sStr == null || sStr.equals(""))
{
return sStr;
}

StringBuffer sTmp = new StringBuffer();
int i = 0;
while (i <= sStr.length()-1)
{
if (sStr.charAt(i) == '\n'||sStr.charAt(i)=='\r')
{
sTmp = sTmp.append("<br>");
} else if (sStr.charAt(i)==' ')
{
sTmp = sTmp.append(" ");
}else
{
sTmp = sTmp.append(sStr.substring(i,i+1));
}
i++;
}
String S1;
S1=sTmp.toString();
return S1;
}
lliang2 2001-11-09
  • 打赏
  • 举报
回复
if (i == -1)
return parentStr;

StringBuffer sb = new StringBuffer();

交换两行可能更好
newhu 2001-11-09
  • 打赏
  • 举报
回复
是的,但是它那个程序有错
southline 2001-11-09
  • 打赏
  • 举报
回复
是到www.52jsp.com上看的吗?
newhu 2001-11-09
  • 打赏
  • 举报
回复
我自己已经解决了这个问题,现帖出来与大家共享
public String replace(String parentStr,String ch,String rep){
int i = parentStr.indexOf(ch);
StringBuffer sb = new StringBuffer();
if (i == -1)
return parentStr;
sb.append(parentStr.substring(0,i) + rep);
if (i+ch.length() < parentStr.length())
sb.append(replace(parentStr.substring(i+ch.length(),parentStr.length()),ch,rep));
return sb.toString();
}
yu0yu0 2001-11-09
  • 打赏
  • 举报
回复
private string escape(String input)
{
if(input==null || input.length()=0)
{
return input;
}
StringBuffer brf=new StringBuffer(input.length+6);
char ch=" ";
for (int i=0;i<input.length();i++)
{
ch=input.charAt(i);
if (ch=='\n')
{
buf.append("<br>");
}
return buf.toString();
}
}
大致上应该是这样的
我也曾尝试过indexof,不过没成功!
newhu 2001-11-09
  • 打赏
  • 举报
回复
find=testchar.indexOf("\n",find+1);
这句话一直不行,
southline 2001-11-09
  • 打赏
  • 举报
回复
我用得挺好
ggzzkk 2001-11-08
  • 打赏
  • 举报
回复
xuexi
langjianjun 2001-11-08
  • 打赏
  • 举报
回复
\13
southline 2001-11-08
  • 打赏
  • 举报
回复
行不行,你说一下
southline 2001-11-08
  • 打赏
  • 举报
回复
<%String testchar=request.getParameter("param");//获得要转换的内容
String contentc="";
int find=0;
int lastfind=0;
while(find!=-1)
{

find=testchar.indexOf("\n",find+1);

// out.print(Integer.toString(find));
if(find==-1)
{
find=testchar.length();
testchar+=testchar.substring(lastfind,find);
find=-1;
}
else{
contentc+=testchar.substring(lastfind,find);
}
contentc+="<br>";
lastfind=find;
}%>
这样应该行了,不明白
有什么问题,可以到www.52jsp.com上去问

???
快来快来快来快来快来快来快来快来快来
newhu 2001-11-08
  • 打赏
  • 举报
回复
这种方法不行,因为在JAVA中它识别的是UNICODE,而UNICODE是2个字节的,所以行不通
园艺爱移动 2001-11-08
  • 打赏
  • 举报
回复
把从数据库中取出的字符串getByte成Byte数据,使用循环把ASCII码值10 和13连在一起出现出转换成<br> (或见到ASCII码值为13(回车)的就换成<BR>) 应该就OK了。
redstarstar 2001-11-08
  • 打赏
  • 举报
回复
读出来后,判断是否为“\r\n”.

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧