简体   繁体   中英

Removing characters from a String: Regular expression or Manually

Which would be a better approach (time and space) for removing characters from a String:

Approach 1 :

String charsToRemove = "abc";
String myString = "abcdef";

myString = myString.replaceAll("["+charsToRemove+"]", "");

Approach 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);

Visually, option 1 is much clearer. This alone is enough to make me choose it. It's also probably faster, as well (though it depends on the lengths of your input). In option two, you're setting up a second loop. HashSet.contains() runs its own loop, adding a second delay.

For short strings, you likely won't notice a difference, but once again, it's hard to tell what's happening in the second.

If the character set is fixed then I would change option 1 as

private static Pattern pattern = Pattern.compile("[abc]");

public String removeChars(String s) {
    return pattern.matcher(s).replaceAll("");
}

String.replaceAll looks nice but internally it calls Pattern.compile each time. Besides "[" + charsToRemove + "]" is a performance killer which in our case can be avoided.

as for option 2

Character[] myCharArray = myString.toCharArray();

won't work, it should be

char[] myCharArray = myString.toCharArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM