Efficiently removing specific characters (some punctuation) from Strings in Java?

后端 未结 7 670
青春惊慌失措
青春惊慌失措 2020-12-10 17:14

In Java, what is the most efficient way of removing given characters from a String? Currently, I have this code:

private static String processWord(String x)          


        
7条回答
  •  粉色の甜心
    2020-12-10 17:42

    Right now your code will iterate over all characters of tmp and compare them with all possible characters that you want to remove, so it will use
    number of tmp characters x number or characters you want to remove comparisons.

    To optimize your code you could use short circuit OR || and do something like

    StringBuilder sb = new StringBuilder();
    for (char c : tmp.toCharArray()) {
        if (!(c == ',' || c == '.' || c == ';' || c == '!' || c == '?'
                || c == '(' || c == ')' || c == '{' || c == '}' || c == '['
                || c == ']' || c == '<' || c == '>' || c == '%'))
            sb.append(c);
    }
    tmp = sb.toString();
    

    or like this

    StringBuilder sb = new StringBuilder();
    char[] badChars = ",.;!?(){}[]<>%".toCharArray();
    
    outer: 
    for (char strChar : tmp.toCharArray()) {
        for (char badChar : badChars) {
            if (badChar == strChar)
                continue outer;// we skip `strChar` since it is bad character
        }
        sb.append(strChar);
    }
    tmp = sb.toString();
    

    This way you will iterate over every tmp characters but number of comparisons for that character can decrease if it is not % (because it will be last comparison, if character would be . program would get his result in one comparison).


    If I am not mistaken this approach is used with character class ([...]) so maybe try it this way

    Pattern p = Pattern.compile("[,.;!?(){}\\[\\]<>%]"); //store it somewhere so 
                                             //you wont need to compile it again
    tmp = p.matcher(tmp).replaceAll("");
    

提交回复
热议问题