Compare using String.Contains () disregarding accents and case

3

I know the

//historico e searchC são ArrayLists, no caso do case utilizei o 
//toUpperCase() para igualar, porém, em questão de acentos ele não retorna 
//nenhum valor, mesmo estando exatamente igual ao valor da linha do array
int i = 0;
    for (String[] linha : historico){
        if(linha[2].contains(nome.toUpperCase())) {
            searchC.add(historico.get(i));

        }
        i++;
    }

If the variable nome contains any type of accent, no value is returned in searchC . Does anyone know how to make the comparison disregarding accents and case directly?

    
asked by anonymous 13.04.2018 / 05:26

2 answers

4

By adapting this SOen response , you can use the Normalizer for this:

public boolean contanisIgnoreAccents(String a, String b) {
    String input1 = Normalizer.normalize(a, Normalizer.Form.NFD)
            .replaceAll("\p{InCombiningDiacriticalMarks}+", "")
            .toLowerCase();

    String input2 = Normalizer.normalize(b, Normalizer.Form.NFD)
            .replaceAll("\p{InCombiningDiacriticalMarks}+", "")
            .toLowerCase();

    return input1.contains(input2);
}

The comparison below:

System.out.println(contanisIgnoreAccents("Este é joao", "João"));
System.out.println(contanisIgnoreAccents("Onde está joÂo", "João"));

will return:

true
true

As can be seen in the ideone: link

The method checks whether the second string passed as an argument is contained in the first.

¹ I have edited the example so that the above definition is clear, because if it is not used in this way, the result may be wrong.

    
13.04.2018 / 13:07
2

I believe the simplest and quickest way would be to use StringUtils from Apache Commons Lang .

In build.gradle add dependency

dependencies {
  ...
  implementation 'org.apache.commons:commons-lang3:3.7'
}

Java code

import org.apache.commons.lang3.StringUtils;

public Boolean contanisIgnoreAccents(String a, String b) {

    // Remove os acentos e convert para minúsculo
    String str1 = StringUtils.stripAccents(a).toLowerCase();
    String str2 = StringUtils.stripAccents(b).toLowerCase();

    return str1.contains(str2);
}

public void onButtonClick {
    System.out.println(contanisIgnoreAccents("João", "joao"));
    System.out.println(contanisIgnoreAccents("João", "joÃo"));
}

Result

true
true

Kotlin Code

fun contanisIgnoreAccents(a: String, b: String) = (
    StringUtils.stripAccents(a).toLowerCase().contains(StringUtils.stripAccents(b).toLowerCase())    
)
    
13.04.2018 / 14:34