Created
August 13, 2020 16:22
-
-
Save httpmurilo/87c249a1f15bbd931bcea734b9b6f7d0 to your computer and use it in GitHub Desktop.
Limpar acentos especiais
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
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