Last active
August 6, 2017 17:38
-
-
Save nidorx/eb186d0049f19b5f3e5ea6b37447c851 to your computer and use it in GitHub Desktop.
Utilitário para substituição de acentos de uma string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.nidorx; | |
import java.util.Map; | |
import java.util.HashMap; | |
public final class StringUtil { | |
private static final Map<String, String> DIACRITCS = new HashMap<>(); | |
static { | |
DIACRITCS.put("á", "a"); | |
DIACRITCS.put("à", "a"); | |
DIACRITCS.put("ã", "a"); | |
DIACRITCS.put("â", "a"); | |
DIACRITCS.put("ä", "a"); | |
DIACRITCS.put("Á", "A"); | |
DIACRITCS.put("À", "A"); | |
DIACRITCS.put("Ã", "A"); | |
DIACRITCS.put("Â", "A"); | |
DIACRITCS.put("Ä", "A"); | |
DIACRITCS.put("é", "e"); | |
DIACRITCS.put("è", "e"); | |
DIACRITCS.put("ê", "e"); | |
DIACRITCS.put("ë", "e"); | |
DIACRITCS.put("É", "E"); | |
DIACRITCS.put("È", "E"); | |
DIACRITCS.put("Ê", "E"); | |
DIACRITCS.put("Ë", "E"); | |
DIACRITCS.put("í", "i"); | |
DIACRITCS.put("ì", "i"); | |
DIACRITCS.put("î", "i"); | |
DIACRITCS.put("ï", "i"); | |
DIACRITCS.put("Í", "I"); | |
DIACRITCS.put("Ì", "I"); | |
DIACRITCS.put("Î", "I"); | |
DIACRITCS.put("Ï", "I"); | |
DIACRITCS.put("ó", "o"); | |
DIACRITCS.put("ò", "o"); | |
DIACRITCS.put("õ", "o"); | |
DIACRITCS.put("ô", "o"); | |
DIACRITCS.put("ö", "o"); | |
DIACRITCS.put("Ó", "O"); | |
DIACRITCS.put("Ò", "O"); | |
DIACRITCS.put("Õ", "O"); | |
DIACRITCS.put("Ô", "O"); | |
DIACRITCS.put("Ö", "O"); | |
DIACRITCS.put("ú", "u"); | |
DIACRITCS.put("ù", "u"); | |
DIACRITCS.put("û", "u"); | |
DIACRITCS.put("ü", "u"); | |
DIACRITCS.put("Ú", "U"); | |
DIACRITCS.put("Ù", "U"); | |
DIACRITCS.put("Û", "U"); | |
DIACRITCS.put("Ü", "U"); | |
DIACRITCS.put("ñ", "n"); | |
DIACRITCS.put("Ñ", "N"); | |
DIACRITCS.put("ç", "c"); | |
DIACRITCS.put("Ç", "C"); | |
DIACRITCS.put("ÿ", "y"); | |
DIACRITCS.put("ý", "y"); | |
DIACRITCS.put("Ý", "Y"); | |
} | |
/** | |
* Faz a substituição de acentos e caracteres especiais | |
* | |
* @param exp {@literal @todo Documentar parâmetro} | |
* @return {@literal @todo Documentar retorno} | |
*/ | |
public static String unaccent(final String exp) { | |
String out = exp; | |
for (Map.Entry<String, String> entry : DIACRITCS.entrySet()) { | |
String key = entry.getKey(); | |
String value = entry.getValue(); | |
out = out.replaceAll(key, value); | |
} | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment