我给你写了个类,你在此基础上修改一下就可以了
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("<"); break;
case '>':filtered.append(">"); break;
case '"':filtered.append("&uot;"); break;
case '&':filtered.append("&"); 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;
}
}
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("<"); break;
case '>':filtered.append(">"); break;
case '"':filtered.append("&uot;"); break;
case '&':filtered.append("&"); 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;
}
}
解决方案 »
- 求教 JAVA中引用传递的问题?
- 本地远程访问DB2数据库连接问题
- Applet JButton纠结问题 无法正常显示 请高手出手呀!
- jsp 如果何向bean 传参数并完成一个数据插入操作。
- 应朋友要求散分,顺便期待小黄金周!!
- hibernate第一次使用就出问题
- 请问 struts 在哪里下载阿~我在http://jakarta.apache.org 这里的 download 下的是1.1 可是兄弟们说 release 版本才 1.0.2 而且我下的1.
- java初学者向高手请教
- 急急急!!!编译JSP后,删掉源文件,该页还能被访问,如何实现?
- get和post提交
- 【求助】在线程中怎么访问父进程的内存?????
- Spring中最简单的XmlBeanFactory问题
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.
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*******楼主问题已经解决,期待给分~
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));
}
}