Search code examples
javastringcomparespecial-characters

Java ignore special characters in string matching


I want to match two strings in java eg.

text: János

searchExpression: Janos

Since I don't want to replace all special characters, I thought I could just make the á a wildcard, so everything would match for this character. For instance if I search in János with Jxnos, it should find it. Of course there could be multiple special characters in the text. Does anyone have an idea how I could achieve this via any pattern matcher, or do I have to compare char by char?


Solution

  • A possible solution would be to strip the accent with the help of Apache Commons StringUtils.stripAccents(input) method:

    String input = StringUtils.stripAccents("János");
    System.out.println(input); //Janos
    

    Make sure to also read upon the more elaborate approaches based on the Normalizer class: Is there a way to get rid of accents and convert a whole string to regular letters?