我想从字符串中删除所有特殊字符,我尝试了 stackoverflow 中给出的许多选项,但没有一个对我有用。
这是我的代码:
public class convert {
public static void main(String[] args) {
try {
List<List<String>> outerList = new ArrayList<List<String>>();
outerList.add(new ArrayList<String>(asList("11-","2")));
outerList.add(new ArrayList<String>(asList("(2^","1")));
outerList.add(new ArrayList<String>(asList("11","3)")));
int i,j;
for(i=0;i<outerList.size();i++){
for(j=0;j<outerList.get(0).size();j++){
outerList.get(i).get(j).replaceAll("[^\\w\\s]", "");
if(outerList.get(i).get(j).matches("-?\\d+"){
continue;
}else{
System.out.println("special characters not removed");
System.exit(0);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
(简单)错误是 s.replaceAll(...)
不会更改 s
但会产生一个新的更改后的字符串:
String s = outerList.get(i).get(j).replaceAll("[^\\w\\s]", "");
outerList.get(i).set(j, s);
关于java - java中如何删除字符串中的所有特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25784493/