字符串过滤类(StrUtil)

package com.jobcn.util;

public class StrUtil {
 public static final String nullToEmpty(String str1) {
  if (str1 == null) {
   str1 = "";
  }
  return (str1);
 }

 //字符串替换 s 搜索字符串 s1 要查找字符串 s2 要替换字符串
 public static final String replace(String s, String s1, String s2) {
  if (s == null) {
   return null;
  }
  int i = 0;
  if ((i = s.indexOf(s1, i)) >= 0) {
   char ac[] = s.toCharArray();
   char ac1[] = s2.toCharArray();
   int j = s1.length();
   StringBuffer stringbuffer = new StringBuffer(ac.length);
   stringbuffer.append(ac, 0, i).append(ac1);
   i += j;
   int k;
   for (k = i; (i = s.indexOf(s1, i)) > 0; k = i) {
    stringbuffer.append(ac, k, i - k).append(ac1);
    i += j;
   }
   stringbuffer.append(ac, k, ac.length - k);
   return stringbuffer.toString();
  } else {
   return s;
  }
 }

 public static final String replaceIllegalString(String str) {
  if (str == null) {
   str = nullToEmpty(str);
  } else {
   str = replaceSpecialString(str);
   str = replace(str, "/", "");
   str = replace(str, "\\", "");
   str = replace(str, "'", "");
   str = replace(str, "\"", "");
  }

  return str;

 }
 
 public static final String replaceSpecialString(String str) {
  if (str == null) {
   str = nullToEmpty(str);
  } else {
   
   str = replace(str, "exec", "");
   str = replace(str, " select ", "");
   str = replace(str, " insert ", "");
   str = replace(str, " delete ", "");
   str = replace(str, " update ", "");
   str = replace(str, " into ", "");
   str = replace(str, " count ", "");
   str = replace(str, " master ", "");
   str = replace(str, " truncate ", "");
   str = replace(str, " drop ", "");
   str = replace(str, " declare ", "");
   str = replace(str, " and ", "");
   str = replace(str, " or ", "");
   str = replace(str, " join ", "");
   str = replace(str, " union ", "");
   str = replace(str, " where ", "");
   str = replace(str, " like ", "");
   str = replace(str, " drop ", "");
   str = replace(str, " create ", "");
   str = replace(str, " rename ", "");
   str = replace(str, " alter ", "");
   str = replace(str, " exists ", "");
   
   
    str = replace(str, "=", "");
   str = replace(str, "", "");
   str = replace(str, "script", "");
   str = replace(str, "object", "");
   str = replace(str, "applet", "");
   str = replace(str, " cmd ", "");
   
  }

  return str;

 }
  public static String toFirstCharUpcase(String s)
     {
         if(s == null || s.length() < 1)
             return s;
         char c[] = s.toCharArray();
         if(c.length > 0 && c[0] >= 'a' && c[0] <= 'z')
             c[0] = (char)((short)c[0] - 32);
         return String.valueOf(c);
     }
 
  public static String toFirstCharLowCase(String s)
     {
         if(s == null || s.length() < 1)
             return s;
         char c[] = s.toCharArray();
         if(c.length > 0 && c[0] >= 'A' && c[0] <= 'Z')
             c[0] = (char)((short)c[0] + 32);
         return String.valueOf(c);
     }
 
 public static String HTMLEncode(String content)
 {
  String contentnew=content;
  String enter="<br/>\n"; 
  contentnew=contentnew.replaceAll("&","&amp;");
  contentnew=contentnew.replaceAll("<","&lt;");
  contentnew=contentnew.replaceAll(" ","&nbsp;");
  contentnew=contentnew.replaceAll(">","&gt;");
  contentnew=contentnew.replaceAll("\"","&quot;");
  contentnew=contentnew.replaceAll("\r\n",enter); 
 
  return contentnew;
 }
 public static String HTMLEncode_edithtml(String content)
 {
  String contentnew=content;
  //String enter="<br/>\n"; 
  contentnew=contentnew.replaceAll("&","&amp;");
  contentnew=contentnew.replaceAll("<","&lt;");
  contentnew=contentnew.replaceAll("   ","&nbsp;");
  contentnew=contentnew.replaceAll(">","&gt;");
  contentnew=contentnew.replaceAll("\"","&quot;");
  //contentnew=contentnew.replaceAll("\r\n",enter); 
 
  return contentnew;
 } 
 
  public static void main(String[] args){
      
  }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值