我给你写了个类,你在此基础上修改一下就可以了
public class FilterSpecial 
{
  
  public static String filter(String input)//传值到这里
  {
    if(!hasSpecialChars(input))//如果没有,就返回原字符串
    { 
    return input;
    }
    StringBuffer filtered =new StringBuffer(input.length());
    char c;
    for(int i=0; i<=input.length()-1; i++)
    {
    c=input.charAt(i);
    switch(c)
    {
      case '<':filtered.append("&lt;"); break;
      case '>':filtered.append("&gt;"); break;
      case '"':filtered.append("&uot;"); break;
      case '&':filtered.append("&amp;"); break;
      default: filtered.append(c);
    }
      
    }
    return (filtered.toString());
  }
  public static boolean hasSpecialChars(String input)//判断特殊字符
  {
    boolean flag=false;
    if((input!=null)&&(input.length()>0))
    {
      char c;
      for(int i=0; i<=input.length()-1; i++)
      {
        c=input.charAt(i);      
        switch(c)
        {
          case '>': flag=true; break;
          case '<': flag=true; break;
          case '"': flag=true; break;
          case '&': flag=true; break;        }
      }
    }
    return flag;
  }
}

解决方案 »

  1.   

    还可以用String 类的replacefrom API1.5:
    public String replace(char oldChar,
                          char newChar)
    Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. 
    If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar. Examples:  "mesquite in your cellar".replace('e', 'o')
             returns "mosquito in your collar"
     "the war of baronets".replace('r', 'y')
             returns "the way of bayonets"
     "sparring with a purple porpoise".replace('p', 't')
             returns "starring with a turtle tortoise"
     "JonL".replace('q', 'x') returns "JonL" (no change)
     Parameters:
    oldChar - the old character.
    newChar - the new character. 
    Returns:
    a string derived from this string by replacing every occurrence of oldChar with newChar.
      

  2.   

    还可以用replaceAll来解决replaceAll
    public String replaceAll(String regex,
                             String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement. 
    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters:
    regex - the regular expression to which this string is to be matched 
    Returns:
    The resulting String 
    Throws: 
    PatternSyntaxException - if the regular expression's syntax is invalid
    Since: 
    1.4 
    See Also:
    Pattern*******楼主问题已经解决,期待给分~
      

  3.   

    Many thanks, I'll have a try.
      

  4.   

    using the regular expression [^a-zA-Z_0-9] together with replaceAll
    method to strip the special chracters. 
    --------------------------------------------
    import java.util.regex.*;public class StringFilter {
    public static String filter(String reg ,String str) throws PatternSyntaxException {
    if(reg == null) 
      return null;
    Pattern p = Pattern.compile(reg);
    Matcher m = p.matcher(str);
    return m.replaceAll("");
    }

    public static void main(String[] args) throws PatternSyntaxException {
    String reg = "[^a-zA-Z_0-9]";
    String str = "*adCVs*345*[/435}34{45[]12";
    System.out.println(str);
    System.out.println(StringFilter.filter(reg,str));
    }
    }