java过滤非法字符的两个方法

public static boolean sql_inj(String str)
{
String inj_str = “‘|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,”;
String inj_stra[] = split(inj_str,”|”);
for (int i=0 ; i < inj_stra.length ; i++ )
{
if (str.indexOf(inj_stra)>=0)
{
return true;
}
}
return false;
}

 

-------------------------------------------------

 

content2//就是提交的内容

public static final String filter=”代开发票|第六世纪|我叫小刘”;//要过虑的字符,用|分隔
java.util.regex.Matcher m=java.util.regex.Pattern.compile(filter).matcher(content2);
if(m.find())
{
out.println(”内容中含用非法字符(不文明用语,广告,政治话题或特殊符号).”);
return;
}

 

 

----------------------------------------------------------------------------------------

 

 

public class Test {
 
 public static void main(String []args){
  
  String test = "色情----暴力----政治----阿斯顿ghj----江泽民蓄势待发----让他v----爱上对方----sadweer231r123----毛泽东";
  ArrayList filter = new ArrayList();
  filter.add("色情");
  filter.add("暴力");
  filter.add("政治");
  filter.add("江泽民");
  filter.add("毛泽东");
  System.out.println(replace(test,filter,"***"));
 }
 
  public static String replace(String StrInfo, ArrayList glossaryList, String shiftStr) {
  String newString = "";
  int first = 0;
  for(Iterator it = glossaryList.iterator() ; it.hasNext();){
   String glossary = it.next().toString();
   while (StrInfo.indexOf(glossary) != -1) {
    first = StrInfo.indexOf(glossary);
    if (first != StrInfo.length()) {
     newString = newString + StrInfo.substring(0, first) + shiftStr;
     StrInfo = StrInfo.substring(first + glossary.length(), StrInfo.length());
    }
   }
  }
  newString = newString + StrInfo;
  return newString;
 } 
}


原文链接: http://www.dlog.cn/nicholascoder/diary/35550013

你可能感兴趣的:(java过滤非法字符的两个方法)