Compare strings ignoring accented characters

alexandre1985 picture alexandre1985 · Mar 3, 2015 · Viewed 21.9k times · Source

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

Answer

Kennedy Oliveira picture Kennedy Oliveira · Mar 3, 2015

You can use java Collators for comparing the tests ignoring the accent, 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'll not explain in details because i used just a little of Collators and i'm not a expert in it, but you can google there's some articles about it.