有一种方法可以过滤您需要的特殊字符。只需检查ASCII表
希望能帮助到你
public class main {
public static void main(String args[]) {
String str = args[0];
String bstr = "";
String[] codePointAt = new String[str.length()];
if (str != "")
{
for (int j = 0; j < str.length(); j++)
{
int charactercode=Character.codePointAt(str, j);
//CHECK on ASCII TABLE THE SPECIAL CHARS YOU NEED
if( (charactercode>31 && charactercode<48) ||
(charactercode>57 && charactercode<65) ||
(charactercode>90 && charactercode<97) ||
(charactercode>127)
)
{
codePointAt[ j] ="&"+String.valueOf(charactercode)+";";
}
else
{
codePointAt[ j] = String.valueOf( str.charAt(j) );
}
}
for (int j = 0; j < codePointAt.length; j++)
{
System.out.println("CODE "+j+" ->"+ codePointAt[j]);
}
}
}
}
输出
call with ("TRY./&asda")
CODE 0 ->T
CODE 1 ->R
CODE 2 ->Y
CODE 3 ->&46;
CODE 4 ->&47;
CODE 5 ->&38;
CODE 6 ->a
CODE 7 ->s
CODE 8 ->d
CODE 9 ->a