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)
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("");