Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - Replace German characters (umlauts, accents) with english equivalents

Replace German characters (umlauts, accents) with english equivalents

I need to remove any german specific characters from various fields of text for processing into another system which wont accept them as valid.

So the characters I am aware of are:

? ? ? ü ? ? ü

At the moment I have a bit of a manual way of replacing them:

myGermanString.Replace("?","a").Replace("?","o").Replace("ü","u").....

But I was hoping there was a simpler / more efficient way of doing it. Since I'll be doing it on thousands of strings per run, 99% of which will not contain these chars.

Maybe a method involving some sort of CultureInfo?

(for example, according to MS, the following returns the strings are equal

String.Compare("Stra?e", "Strasse", StringComparison.CurrentCulture);

so there must be some sort of conversion table already existing?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The process is known as removing "diacritics" - see Removing diacritics (accents) from strings which uses the following code:

public static String RemoveDiacritics(String s)
{
  String normalizedString = s.Normalize(NormalizationForm.FormD);
  StringBuilder stringBuilder = new StringBuilder();

  for (int i = 0; i < normalizedString.Length; i++)
  {
    Char c = normalizedString[i];
    if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
      stringBuilder.Append(c);
  }

  return stringBuilder.ToString();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...