2

I want to compare (case insensitive) two strings ignoring punctuation marks like dots, commas or other symbols (like spanish or french letters ó ü ê etc)

Some examples:

  Hello == Helló
  Hello, Sam == Hello sam
  geo-code   == geocode == geo code

(Is not necessary to cover all the options)

Whats the best solution (external libraries, methods, etc)

Addev
  • 28,535
  • 43
  • 166
  • 288

1 Answers1

5

Remove all special characters.

Like from geo-code remove - it becomes geocode. Similarly, remove space from geo code

Hello, sam after removing space and comma becomes Hellosam then you can compare these strings. Here's how you can do that.

Now about the special characters like French ones: This will help you.

Here's the code found in another thread. I have not tested it.

import java.text.Normalizer;
import java.util.regex.Pattern;

public String deAccent(String str) {
    String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD); 
    Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    return pattern.matcher(nfdNormalizedString).replaceAll("");
}
Community
  • 1
  • 1
Suraj Bajaj
  • 6,460
  • 4
  • 33
  • 49