我正在开发一个分析文本的短程序,但是我必须从Arraylist中删除所有使用过的字母的所有spezial字母。
我该怎么做? 我的三:
String[] SpezialLetters = {"1"," ","2","3","4","5","6","7","8","9","0","^","°","!","§","$","%","&","/","{","(","[",")","]","=","}","?","´","`","+","*","~","#","'","<",">","|","@","€",",",";",".",":","-","_"};
List<String> Letters = new ArrayList<String>(Arrays.asList(textToAnalyse.split("")));
for(String Letter : Letters){
if (Arrays.asList(SpezialLetters) != null && Arrays.asList(SpezialLetters).contains(Letter)){
Letters.remove(Letter);
}
}
答案 0 :(得分:1)
letters.removeAll(Arrays.asList(spezialLetters));
应该这样做。
答案 1 :(得分:0)
试试这个:
for (int i = 0; i < letters.size(); i++) {
if (letters.get(i).replaceAll("[^a-zA-Z0-9]+","").equals("")){
letters.remove(i);
}
}
答案 2 :(得分:0)
而不是在List
中创建文字的Strings
个字符textToAnalyse
。以下代码段不是更好的解决方案吗?
String toRemove = "[ \\d\\^°!§$%&/{}()\\[\\]=\\?´`\\+\\*~#'<>\\|@€,;\\.:\\-_]";
String textToAnalyse = "some f00 un _-:.;,€@|><'#~*+`´?}=])[({/&%$§!°^0987654321 ";
String cleaned = textToAnalyse.replaceAll(toRemove, "");
System.out.printf("toRemove : [%s]%n", toRemove);
System.out.printf("textToAnalyse: [%s]%n", textToAnalyse);
System.out.printf("cleaned : [%s]%n", cleaned);
<强>输出强>
toRemove : [ \d\^°!§$%&/{}()\[\]=\?´`\+\*~#'<>\|@€,;\.:\-_]
textToAnalyse: [some f00 un _-:.;,€@|><'#~*+`´?}=])[({/&%$§!°^0987654321 ]
cleaned : [somefun]
toRemove
模式的含义:
\d
- 匹配所有数字^[]?+*|.-
- 这些字符在正则表达式pattern中具有特殊含义,因此必须转义all others
- 匹配文字字符