Compare strings ignoring accented characters

I would like to know if there is a method that compares 2 lines and ignores accents making "noção" equal to "nocao". it will be something like string1.methodCompareIgnoreAccent (string2);

+6
source share
2 answers

You can use java Collators to compare tests with ignoring emphasis, see a simple example:

import java.text.Collator; /** * @author Kennedy */ public class SimpleTest { public static void main(String[] args) { String a = "nocao"; String b = "noção"; final Collator instance = Collator.getInstance(); // This strategy mean it'll ignore the accents instance.setStrength(Collator.NO_DECOMPOSITION); // Will print 0 because its EQUAL System.out.println(instance.compare(a, b)); } } 

Documentation: JavaDoc

I will not explain in detail, because I used only a few Collators, and I am not an expert in this, but you can find some articles in it about it.

+17
source

There is no built-in method for this, so you need to create your own:

Part of this decision is here : This first divides all the accented characters into their de-ambient colleagues, and then their combined diacritics. Then you simply delete all combinations of diacritics. Also see fooobar.com/questions/38387 / ...

And then your equals method will look like this:

 import java.text.Normalizer; import java.text.Normalizer.Form; public boolean equals(Object o) { // Code omitted if (yourField.equals(removeAccents(anotherField))) { return true; } } public static String removeAccents(String text) { return text == null ? null : Normalizer.normalize(text, Form.NFD) .replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); } 
+3
source

Source: https://habr.com/ru/post/983265/


All Articles