terça-feira, 21 de agosto de 2012

Retirar Acentos



Existem ocasiões em que será necessário remover acentos das informações digitadas pelos usuários.

Para este exemplo utilizaremos os seguintes controles aspx:
  •  Button
  • TextBox

Adicione o código abaixo em um controle do tipo Button

string texto;

texto = textBox1.Text;

textBox1.Text = TirarAcentos(texto);



public string TirarAcentos(string texto)
{
            string textor = "";

            for (int i = 0; i < texto.Length; i++)
            {

                if (texto[i].ToString() == "ã") textor += "a";

                else if (texto[i].ToString() == "á") textor += "a";

                else if (texto[i].ToString() == "à") textor += "a";

                else if (texto[i].ToString() == "â") textor += "a";

                else if (texto[i].ToString() == "ä") textor += "a";

                else if (texto[i].ToString() == "é") textor += "e";

                else if (texto[i].ToString() == "è") textor += "e";

                else if (texto[i].ToString() == "ê") textor += "e";

                else if (texto[i].ToString() == "ë") textor += "e";

                else if (texto[i].ToString() == "í") textor += "i";

                else if (texto[i].ToString() == "ì") textor += "i";

                else if (texto[i].ToString() == "ï") textor += "i";

                else if (texto[i].ToString() == "õ") textor += "o";

                else if (texto[i].ToString() == "ó") textor += "o";

                else if (texto[i].ToString() == "ò") textor += "o";

                else if (texto[i].ToString() == "ö") textor += "o";

                else if (texto[i].ToString() == "ú") textor += "u";

                else if (texto[i].ToString() == "ù") textor += "u";

                else if (texto[i].ToString() == "ü") textor += "u";

                else if (texto[i].ToString() == "ç") textor += "c";

                else if (texto[i].ToString() == "Ã") textor += "A";

                else if (texto[i].ToString() == "Á") textor += "A";

                else if (texto[i].ToString() == "À") textor += "A";

                else if (texto[i].ToString() == "Â") textor += "A";

                else if (texto[i].ToString() == "Ä") textor += "A";

                else if (texto[i].ToString() == "É") textor += "E";

                else if (texto[i].ToString() == "È") textor += "E";

                else if (texto[i].ToString() == "Ê") textor += "E";

                else if (texto[i].ToString() == "Ë") textor += "E";

                else if (texto[i].ToString() == "Í") textor += "I";

                else if (texto[i].ToString() == "Ì") textor += "I";

                else if (texto[i].ToString() == "Ï") textor += "I";

                else if (texto[i].ToString() == "Õ") textor += "O";

                else if (texto[i].ToString() == "Ó") textor += "O";

                else if (texto[i].ToString() == "Ò") textor += "O";

                else if (texto[i].ToString() == "Ö") textor += "O";

                else if (texto[i].ToString() == "Ú") textor += "U";

                else if (texto[i].ToString() == "Ù") textor += "U";

                else if (texto[i].ToString() == "Ü") textor += "U";

                else if (texto[i].ToString() == "Ç") textor += "C";

                else textor += texto[i];

            }

            return textor;           
}

Nenhum comentário:

Postar um comentário