文本区域 换符号

来源:互联网 发布:网吧服务器攻击软件 编辑:程序博客网 时间:2024/04/29 20:22

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,"<","&lt;");

   s = Replace(s,">","&gt;");

   s = Replace(s,"&","&amp;");

   s = Replace(s,"/t","    ");

   s = Replace(s,"/r/n","/n");

   s = Replace(s,"/n","<br />");

   s = Replace(s,"  "," &nbsp;");

   s = Replace(s,"'","&#39;");

   s = Replace(s,"//","&#92;");

   return s;

}

public static String unHtml(String s)

{

s = Replace(s,"&nbsp;"," ");

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");

 

 

 

原创粉丝点击