Skip to content

Instantly share code, notes, and snippets.

@sergiocasero
Created December 14, 2017 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiocasero/a645dade18d9cdedaf1f276809f10554 to your computer and use it in GitHub Desktop.
Save sergiocasero/a645dade18d9cdedaf1f276809f10554 to your computer and use it in GitHub Desktop.
Normalize extensions function
fun String.normalize(): String {
val map = mutableMapOf<Char, Char>()
map.put('À', 'A')
map.put('Á', 'A')
map.put('Â', 'A')
map.put('Ã', 'A')
map.put('Ä', 'A')
map.put('È', 'E')
map.put('É', 'E')
map.put('Ê', 'E')
map.put('Ë', 'E')
map.put('Í', 'I')
map.put('Ì', 'I')
map.put('Î', 'I')
map.put('Ï', 'I')
map.put('Ù', 'U')
map.put('Ú', 'U')
map.put('Û', 'U')
map.put('Ü', 'U')
map.put('Ò', 'O')
map.put('Ó', 'O')
map.put('Ô', 'O')
map.put('Õ', 'O')
map.put('Ö', 'O')
map.put('Ñ', 'N')
map.put('Ç', 'C')
map.put('ª', 'A')
map.put('º', 'O')
map.put('§', 'S')
map.put('³', '3')
map.put('²', '2')
map.put('¹', '1')
map.put('à', 'a')
map.put('á', 'a')
map.put('â', 'a')
map.put('ã', 'a')
map.put('ä', 'a')
map.put('è', 'e')
map.put('é', 'e')
map.put('ê', 'e')
map.put('ë', 'e')
map.put('í', 'i')
map.put('ì', 'i')
map.put('î', 'i')
map.put('ï', 'i')
map.put('ù', 'u')
map.put('ú', 'u')
map.put('û', 'u')
map.put('ü', 'u')
map.put('ò', 'o')
map.put('ó', 'o')
map.put('ô', 'o')
map.put('õ', 'o')
map.put('ö', 'o')
map.put('ñ', 'n')
map.put('ç', 'c')
val sb = StringBuilder(this)
for (index in 0 until sb.length) {
val character = map[sb[index]]
if (character != null) {
sb.setCharAt(index, character.toChar())
}
}
return sb.toString()
}
@ffgiraldez
Copy link

have you tried

private static final Pattern DIACRITICS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
public static String removeDiacritics(String value) {
    if (TextUtils.isEmpty(value)) {
        return value;
    }

    String normalized = Normalizer.normalize(value, Normalizer.Form.NFD);
    return DIACRITICS.matcher(normalized).replaceAll("");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment