public static String Replace(String str_source,String str_original,String str_new) {
if(str_source == null)
return null;
StringBuffer output = new StringBuffer();
int lengOfsource = str_source.length();
int lengOfold = str_original.length();
int posStart = 0;
int pos;
while((pos = str_source.indexOf(str_original,posStart)) >= 0) {
output.append(str_source.substring(posStart,pos));
output.append(str_new);
posStart = pos + lengOfold;
}
if(posStart < lengOfsource) {
output.append(str_source.substring(posStart));
}
return output.toString();
}
public static String toHtml(String s)
{
s = Replace(s,"<","<");
s = Replace(s,">",">");
s = Replace(s,"&","&");
s = Replace(s,"/t"," ");
s = Replace(s,"/r/n","/n");
s = Replace(s,"/n","<br />");
s = Replace(s," "," ");
s = Replace(s,"'","'");
s = Replace(s,"//","\");
return s;
}
public static String unHtml(String s)
{
s = Replace(s," "," ");
s = Replace(s,"<br>","/n");
return s;
}
使用如下:
<textarea rows="6" cols="45" id="content" >we have a long way to go</textarea>
toHtml("we have a long way to go");