How do I remove accents in a string?
I have a string
áéíóú
Which I want to convert to
aeiou
How do I remove accents? I need to save in the database as a URL.
7 answers
You can use this function:
public static string RemoveAccents(this string text){
StringBuilder sbReturn = new StringBuilder();
var arrayText = text.Normalize(NormalizationForm.FormD).ToCharArray();
foreach (char letter in arrayText){
if (CharUnicodeInfo.GetUnicodeCategory(letter) != UnicodeCategory.NonSpacingMark)
sbReturn.Append(letter);
}
return sbReturn.ToString();
}
Source: http://www.ninjacode.com.br/post/2011/08/10/Retirar-acentos-de-strings-C.aspx
You can also read all the characters that are in the variable comAcentos
, and given a Replace
in the parameter that was passed in the function, that is, the letters that are comAcentos
are replaced by those of semAcentos
and returns the new text.
public static string removerAcentos(string texto)
{
string comAcentos = "ÄÅÁÂÀÃäáâàãÉÊËÈéêëèÍÎÏÌíîïìÖÓÔÒÕöóôòõÜÚÛüúûùÇç";
string semAcentos = "AAAAAAaaaaaEEEEeeeeIIIIiiiiOOOOOoooooUUUuuuuCc";
for (int i = 0; i < comAcentos.Length; i++)
{
texto = texto.Replace(comAcentos[i].ToString(), semAcentos[i].ToString());
}
return texto;
}
Using LINQ is very practical:
public static string RemoverAcentuacao(this string text)
{
return new string(text
.Normalize(NormalizationForm.FormD)
.Where(ch => char.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
.ToArray());
}
What are NormalizationForm.FormD
and UnicodeCategory.NonSpacingMark
This is a way of representing the original string so that marks such as accentuation, Cedilla, among others, are separated into distinct characters: the base character, which is the letter, and the mark character. The accent character, in this case is called NonSpacingMark
, that is, marker without space, means that it is a marker that does not occupy any space, and will be applied to the character previous.
Using LINQ we can remove these markings, leaving only the base characters, without the markings and we build a new string from these characters.
public static string RemoverAcentos(this string texto)
{
if (string.IsNullOrEmpty(texto))
return String.Empty;
byte[] bytes = System.Text.Encoding.GetEncoding("iso-8859-8").GetBytes(texto);
return System.Text.Encoding.UTF8.GetString(bytes);
}
string nome = "João Felipe Portela";
string nomeSemAcentos = nome.RemoverAcentos();
Is there this method I use to remove accentuation:
public static string RemoverAcentos(string texto){
string s = texto.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < s.Length; k++)
{
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(s[k]);
if (uc != UnicodeCategory.NonSpacingMark)
{
sb.Append(s[k]);
}
}
return sb.ToString();
}
An alternative the answers given above is to install the following nuget: https://www.nuget.org/packages/MMLib.Extensions /
Then you can remove the accents as follows:
var str = "áéíóú";
str = str.RemoveDiacritics();
The codepage Greek (ISO) can do this
Information about this codepage can be obtained in the return method System.Text.Encoding.GetEncodings()
. See more here .
Greek (ISO) has the codepage 28597 and name iso-8859-7.
Let's go to the code... \o /
string text = "Você está numa situação lamentável";
string textEncode = System.Web.HttpUtility.UrlEncode(text, Encoding.GetEncoding("iso-8859-7"));
//result: "Voce+esta+numa+situacao+lamentavel"
string textDecode = System.Web.HttpUtility.UrlDecode(textEncode);
//result: "Voce esta numa situacao lamentavel"
Then write this function...
public string RemoverAcentuacao(string text)
{
return
System.Web.HttpUtility.UrlDecode(
System.Web.HttpUtility.UrlEncode(
text, Encoding.GetEncoding("iso-8859-7")));
}
Note that Encoding.GetEncoding("iso-8859-7")
is equivalent to Encoding.GetEncoding(28597)
. The first search by name, the second by the codepage of Encoding.
Other options can be seen in Stackoverflow in English :