随笔-211  评论-26  文章-8  trackbacks-0
// 替换字符串函数
 // String strSource - 源字符串
 // String strFrom  - 要替换的子串
 // String strTo   - 替换为的字符串
 public static String replace(String strSource, String strFrom, String strTo)
 {
   // 如果要替换的子串为空,则直接返回源串
   if(strFrom == null || strFrom.equals(""))
     return strSource;
   String strDest = "";
   // 要替换的子串长度
   int intFromLen = strFrom.length();
   int intPos;
   // 循环替换字符串
   while((intPos = strSource.indexOf(strFrom)) != -1)
   {
     // 获取匹配字符串的左边子串
     strDest = strDest + strSource.substring(0,intPos);
     // 加上替换后的子串
     strDest = strDest + strTo;
     // 修改源串为匹配子串后的子串
     strSource = strSource.substring(intPos + intFromLen);
   }
   // 加上没有匹配的子串
   strDest = strDest + strSource;
   // 返回
   return strDest;
 } 

public   static   String   toHTMLString(String   in)   {  
                  StringBuffer   out   =   new   StringBuffer();  
                  for   (int   i   =   0;   in   !=   null   &&   i   <   in.length();   i++)   {  
                          char   c   =   in.charAt(i);  
                          if   (c   ==   '\'')  
                                  out.append("'");  
                          else   if   (c   ==   '\"')  
                                  out.append(""");  
                          else   if   (c   ==   '<')  
                                  out.append("&lt;");  
                          else   if   (c   ==   '>')  
                                  out.append("&gt;");  
                          else   if   (c   ==   '&')  
                                  out.append("&amp;");  
                          else   if   (c   ==   '   ')  
                                  out.append("&nbsp;");  
                          else   if   (c   ==   '\n')  
                                  out.append("<br/>");  
                          else  
                                  out.append(c);  
                  }  
                  return   out.toString();  
          }

s[1] = replace(s[1],"&lt;","<");
   s[1] = replace(s[1],"&gt;",">");
   s[1] = replace(s[1],"&quot;","");
posted on 2008-04-10 09:42 dragon 阅读(1377) 评论(0)  编辑  收藏 所属分类: jsp