Skip to content

Instantly share code, notes, and snippets.

@httpmurilo
Created August 13, 2020 16:22
Show Gist options
  • Save httpmurilo/87c249a1f15bbd931bcea734b9b6f7d0 to your computer and use it in GitHub Desktop.
Save httpmurilo/87c249a1f15bbd931bcea734b9b6f7d0 to your computer and use it in GitHub Desktop.
Limpar acentos especiais
public string ObterStringSemAcentosECaracteresEspeciais(string str)
{
/** Troca os caracteres acentuados por não acentuados **/
string[] acentos = new string[] { "ç", "Ç", "á", "é", "í", "ó", "ú", "ý", "Á", "É", "Í", "Ó", "Ú", "Ý", "à", "è", "ì", "ò", "ù", "À", "È", "Ì", "Ò", "Ù", "ã", "õ", "ñ", "ä", "ë", "ï", "ö", "ü", "ÿ", "Ä", "Ë", "Ï", "Ö", "Ü", "Ã", "Õ", "Ñ", "â", "ê", "î", "ô", "û", "Â", "Ê", "Î", "Ô", "Û" };
string[] semAcento = new string[] { "c", "C", "a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y", "a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "a", "o", "n", "a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "A", "O", "N", "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };
for (int i = 0; i < acentos.Length; i++)
{
str = str.Replace(acentos[i], semAcento[i]);
}
/** Troca os caracteres especiais da string por "" **/
string[] caracteresEspeciais = { "\\.", ",", "-", ":", "\\(", "\\)", "ª", "\\|", "\\\\", "°" };
for (int i = 0; i < caracteresEspeciais.Length; i++)
{
str = str.Replace(caracteresEspeciais[i], "");
}
/** Troca os espaços no início por "" **/
str = str.Replace("^\\s+", "");
/** Troca os espaços no início por "" **/
str = str.Replace("\\s+$", "");
/** Troca os espaços duplicados, tabulações e etc por " " **/
str = str.Replace("\\s+", " ");
return str;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment