1

Im asking me if my code is a common and most efficient way for a custom filter. The user can choose which characters can be used/notUsed. The dirty text can be very long so i have to see that my code needs to be efficient as possible:

    String dirtyText = "iamacleantext<>>";
    String allowedCharacters = "abcdefhijk$<>/lmnoqrgstuvwxyz";

    String result = dirtyText.replaceAll("[" + allowedCharacters + "]","");

    if (result.isEmpty()) {
        System.out.println("Ok, your text can be used");
    } else {
        System.out.println("Sorry the text contains not allowed characters");
    }

would be thankful for someone who has more knowledge about this

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129

1 Answers1

2

There have been many questions regards String.contains vs. regex. According to most of the threads it's clear that regex performance are poorer. An alternative approach (which bails out on the first illegal character):

private static boolean check(String dirtyText) {
    String allowedCharacters = "abcdefhijk$<>/lmnoqrgstuvwxyz";
    for (int i=0; i < dirtyText.length(); i++) {
        if (!allowedCharacters.contains(dirtyText.substring(i, i+1))) {
            return false;
        }
    }
    return true;
}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    This is better than OP's solution because it will fail as soon as an invalid character is found. If you have a string of one million invalid characters, that becomes significant. – Michael Aug 15 '17 at 23:01
  • in fact! regex took 1350016 ns and contains just took 189994 ns. Thank you very much – Berat Çakır Aug 15 '17 at 23:06
  • @Michael That's only because OP is replacing the entire string first. `String.matches()` would probably perform much better. – shmosel Aug 15 '17 at 23:15