For .NET users the article in CodeProject (thanks to GvS's tip) does indeed answer the question more correctly than any other I've seen so far.
However the code in that article (in solution #1) is cumbersome. Here's a compact version:
// Based on http://www.codeproject.com/Articles/13503/Stripping-Accents-from-Latin-Characters-A-Foray-in
private static string LatinToAscii(string inString)
{
var newStringBuilder = new StringBuilder();
newStringBuilder.Append(inString.Normalize(NormalizationForm.FormKD)
.Where(x => x < 128)
.ToArray());
return newStringBuilder.ToString();
}
To expand a bit on the answer, this method uses String.Normalize which:
Returns a new string whose textual value is the same as this string,
but whose binary representation is in the specified Unicode
normalization form.
Specifically in this case we use the NormalizationForm FormKD
, described in those same MSDN docs as such:
FormKD - Indicates that a Unicode string is normalized using full compatibility decomposition.
For more information about unicode normalization forms, see Unicode Annex #15.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…