How do I remove diacritics (accents) from a string in .NET?

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

I'm trying to convert some strings that are in French Canadian and basically, I'd like to be able to take out the French accent marks in the letters while keeping the letter. (E.g. convert é to e, so would become creme brulee)

What is the best method for achieving this?

回答1:

I've not used this method, but Michael Kaplan describes a method for doing so in his blog post (with a confusing title) that talks about stripping diacritics: Stripping is an interesting job (aka On the meaning of meaningless, aka All Mn characters are non-spacing, but some are more non-spacing than others)

static string RemoveDiacritics(string text)  {     var normalizedString = text.Normalize(NormalizationForm.FormD);     var stringBuilder = new StringBuilder();      foreach (var c in normalizedString)     {         var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);         if (unicodeCategory != UnicodeCategory.NonSpacingMark)         {             stringBuilder.Append(c);         }     }      return stringBuilder.ToString().Normalize(NormalizationForm.FormC); } 

Note that this is a followup to his earlier post: Stripping diacritics....

The approach uses String.Normalize to split the input string into constituent glyphs (basically separating the "base" characters from the diacritics) and then scans the result and retains only the base characters. It's just a little complicated, but really you're looking at a complicated problem.

Of course, if you're limiting yourself to French, you could probably get away with the simple table-based approach in How to remove accents and tilde in a C++ std::string, as recommended by @David Dibben.



回答2:

this did the trick for me...

string accentedStr; byte[] tempBytes; tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(accentedStr); string asciiStr = System.Text.Encoding.UTF8.GetString(tempBytes); 

quick&short!



回答3:

In case someone is interested, I was looking for something similar and ended writing the following:

    public static string NormalizeStringForUrl(string name)     {         String normalizedString = name.Normalize(NormalizationForm.FormD);         StringBuilder stringBuilder = new StringBuilder();          foreach (char c in normalizedString)         {             switch (CharUnicodeInfo.GetUnicodeCategory(c))             {                 case UnicodeCategory.LowercaseLetter:                 case UnicodeCategory.UppercaseLetter:                 case UnicodeCategory.DecimalDigitNumber:                     stringBuilder.Append(c);                     break;                 case UnicodeCategory.SpaceSeparator:                 case UnicodeCategory.ConnectorPunctuation:                 case UnicodeCategory.DashPunctuation:                     stringBuilder.Append('_');                     break;             }         }         string result = stringBuilder.ToString();         return String.Join("_", result.Split(new char[] { '_' }             , StringSplitOptions.RemoveEmptyEntries)); // remove duplicate underscores     } 


回答4:

In case anyone's interested, here is the java equivalent:

import java.text.Normalizer;  public class MyClass {     public static String removeDiacritics(String input)     {         String nrml = Normalizer.normalize(input, Normalizer.Form.NFD);         StringBuilder stripped = new StringBuilder();         for (int i=0;i<nrml.length();++i)         {             if (Character.getType(nrml.charAt(i)) != Character.NON_SPACING_MARK)             {                 stripped.append(nrml.charAt(i));             }         }         return stripped.toString();     } } 


回答5:

I often use an extenstion method based on another version I found here (see Replacing characters in C# (ascii)) A quick explanation:

  • Normalizing to form D splits charactes like è to an e and a nonspacing `
  • From this, the nospacing characters are removed
  • The result is normalized back to form C (I'm not sure if this is neccesary)

Code:

using System.Linq; using System.Text; using System.Globalization;  // namespace here public static class Utility {     public static string RemoveDiacritics(this string str)     {         if (null == str) return null;         var chars =             from c in str.Normalize(NormalizationForm.FormD).ToCharArray()             let uc = CharUnicodeInfo.GetUnicodeCategory(c)             where uc != UnicodeCategory.NonSpacingMark             select c;          var cleanStr = new string(chars.ToArray()).Normalize(NormalizationForm.FormC);          return cleanStr;     }      // or, alternatively     public static string RemoveDiacritics2(this string str)     {         if (null == str) return null;         var chars = str             .Normalize(NormalizationForm.FormD)             .ToCharArray()             .Where(c=> CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)             .ToArray();          return new string(chars).Normalize(NormalizationForm.FormC);     } } 


回答6:

I needed something that converts all major unicode characters and the voted answer leaved a few out so I've created a version of CodeIgniter's convert_accented_characters($str) into C# that is easily customisable:

Usage



回答7:

The CodePage of Greek (ISO) can do it

The information about this codepage is into System.Text.Encoding.GetEncodings(). Learn about in: https://msdn.microsoft.com/pt-br/library/system.text.encodinginfo.getencoding(v=vs.110).aspx

Greek (ISO) has codepage 28597 and name iso-8859-7.

Go to the code... \o/

So, write this function...

public string RemoveAcentuation(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) because first is the name, and second the codepage of Encoding.



回答8:

This works fine in java.

It basically converts all accented characters into their deAccented counterparts followed by their combining diacritics. Now you can use a regex to strip off the diacritics.

import java.text.Normalizer; import java.util.regex.Pattern;  public String deAccent(String str) {     String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);      Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");     return pattern.matcher(nfdNormalizedString).replaceAll(""); } 


回答9:

THIS IS THE VB VERSION (Works with GREEK) :

Imports System.Text

Imports System.Globalization

Public Function RemoveDiacritics(ByVal s As String)     Dim normalizedString As String     Dim stringBuilder As New StringBuilder     normalizedString = s.Normalize(NormalizationForm.FormD)     Dim i As Integer     Dim c As Char     For i = 0 To normalizedString.Length - 1         c = normalizedString(i)         If CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark Then             stringBuilder.Append(c)         End If     Next     Return stringBuilder.ToString() End Function 


回答10:

This is how i replace diacritic characters to non-diacritic ones in all my .NET program

C#:

//Transforms the culture of a letter to its equivalent representation in the 0-127 ascii table, such as the letter 'é' is substituted by an 'e' public string RemoveDiacritics(string s) {     string normalizedString = null;     StringBuilder stringBuilder = new StringBuilder();     normalizedString = s.Normalize(NormalizationForm.FormD);     int i = 0;     char c = '\0';      for (i = 0; i <= normalizedString.Length - 1; i++)     {         c = normalizedString[i];         if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)         {             stringBuilder.Append(c);         }     }      return stringBuilder.ToString().ToLower(); } 

VB .NET:

'Transforms the culture of a letter to its equivalent representation in the 0-127 ascii table, such as the letter "é" is substituted by an "e"' Public Function RemoveDiacritics(ByVal s As String) As String     Dim normalizedString As String     Dim stringBuilder As New StringBuilder     normalizedString = s.Normalize(NormalizationForm.FormD)     Dim i As Integer     Dim c As Char      For i = 0 To normalizedString.Length - 1         c = normalizedString(i)         If CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark Then             stringBuilder.Append(c)         End If     Next     Return stringBuilder.ToString().ToLower() End Function 


回答11:

you can use string extension from MMLib.Extensions nuget package:


Nuget page: https://www.nuget.org/packages/MMLib.Extensions/ Codeplex project site https://mmlib.codeplex.com/



回答12:

It's funny such a question can get so many answers, and yet none fit my requirements :) There are so many languages around, a full language agnostic solution is AFAIK not really possible, as others has mentionned that the FormC or FormD are giving issues.

Since the original question was related to French, the simplest working answer is indeed

    public static string ConvertWesternEuropeanToASCII(this string str)     {         return Encoding.ASCII.GetString(Encoding.GetEncoding(1251).GetBytes(str));     } 

1251 should be replaced by the encoding code of the input language.

This however replace only one character by one character. Since I am also working with German as input, I did a manual convert

It might not deliver the best performance, but at least it is very easy to read and extend. Regex is a NO GO, much slower than any char/string stuff.

I also have a very simple method to remove space:

    public static string RemoveSpace(this string str)     {         return str.Replace(" ", string.Empty);     } 

Eventually, I am using a combination of all 3 above extensions:

    public static string LatinizeAndConvertToASCII(this string str, bool keepSpace = false)     {         str = str.LatinizeGermanCharacters().ConvertWesternEuropeanToASCII();                     return keepSpace ? str : str.RemoveSpace();     } 

And a small unit test to that (not exhaustive) which pass successfully.



回答13:

Try HelperSharp package.

There is a method RemoveAccents:

 public static string RemoveAccents(this string source)  {      //8 bit characters       byte[] b = Encoding.GetEncoding(1251).GetBytes(source);       // 7 bit characters      string t = Encoding.ASCII.GetString(b);      Regex re = new Regex("[^a-zA-Z0-9]=-_/");      string c = re.Replace(t, " ");      return c;  } 


回答14:

What this person said:

Encoding.ASCII.GetString(Encoding.GetEncoding(1251).GetBytes(text));

It actually splits the likes of which is one character (which is character code 00E5, not 0061 plus the modifier 030A which would look the same) into a plus some kind of modifier, and then the ASCII conversion removes the modifier, leaving the only a.



回答15:

Popping this Library here if you haven't already considered it. Looks like there are a full range of unit tests with it.

https://github.com/thomasgalliker/Diacritics.NET



回答16:

Imports System.Text Imports System.Globalization   Public Function DECODE(ByVal x As String) As String         Dim sb As New StringBuilder         For Each c As Char In x.Normalize(NormalizationForm.FormD).Where(Function(a) CharUnicodeInfo.GetUnicodeCategory(a) <> UnicodeCategory.NonSpacingMark)               sb.Append(c)         Next         Return sb.ToString()     End Function 


回答17:

I really like the concise and functional code provided by azrafe7. So, I have changed it a little bit to convert it to an extension method:

public static class StringExtensions {     public static string RemoveDiacritics(this string text)     {         const string SINGLEBYTE_LATIN_ASCII_ENCODING = "ISO-8859-8";          if (string.IsNullOrEmpty(text))         {             return string.Empty;         }          return Encoding.ASCII.GetString(             Encoding.GetEncoding(SINGLEBYTE_LATIN_ASCII_ENCODING).GetBytes(text));     } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!