Chinaunix
标题:
将TextArea中的字符转换成HTML格式
[打印本页]
作者:
wangq1979
时间:
2005-01-04 15:50
标题:
将TextArea中的字符转换成HTML格式
经常会用到将TextArea中输入的字符保存到database中,然后从database中读出来,并且在HTML页面上显示,但是TextArea中的字符与HTML中的字符有些不同,特别是换行字符,在TextArea中为“rn”,而HTML中是
,如果不经过转换,在HTML中显示的时候文字内容就会排成一行。下面这个类就是为了把TextArea的换行符转换成HTML的换行符。
[color="#0000ff"]public final class ToolKit {[color="#0000ff"] public static String toHtml(String s){
s=Replace(s,"",">");
s=Replace(s,"t"," ");
s=Replace(s,"n","
");
s=Replace(s," "," ");
return 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=0; int pos;
while((pos=source.indexOf(oldString,posStart))>=0){
output.append(source.substring(posStart,pos));
output.append(newString);
posStart=pos+lengOfOld;
}
if(posStart
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u/1168/showart_9273.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2