这将是从字符串中删除字符的更好方法(时间和空间):
方法1:
String charsToRemove = "abc";
String myString = "abcdef";
myString = myString.replaceAll("["+charsToRemove+"]", "");
方法2:
// Initialized to charsToRemove
HashSet<Character> charsToRemoveSet = ...
Character[] myCharArray = myString.toCharArray();
int dst = 0;
for(int src=0; src<myCharArray.length; src++) {
if(!charsToRemoveSet.contains(myCharArray[src]))
myCharArray[dst++] = myCharArray[src];
}
myString = new String(myCharArray, 0, dst);