Ever wondered how you can translate European national characters with diacritics such as å, ä, ö, ó etc into their base characters (a and o)? This can be useful for example when constructing filenames from user-given strings.
This is one way to do it:
Console.WriteLine("Composite character: " + s[0]);
if (normalizedString.Length> 1)
{
Console.WriteLine("Base character: " + normalizedString[0]);
Console.WriteLine("Diacritic character: " + normalizedString[1]);
}
The result will be:
Obviously, the key is the String.Normalize function. When we pass it NormalizationForm.FormD as the requested Unicode normalization form, it will separate all composite characters into their constituents. If the char is not a composite, then nothing will happen to it.
Note that the resulting string will be longer that the original if characters were separated. If needed it's easy to iterate over the characters to filter our non-letters using conditions such as
Happy character-mangling!
[powered by WordPress.]
jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.
36 queries. 0.363 seconds