Reverse a string with accent chars?

The problem is that Array.Reverse isn’t aware that certain sequences of char values may combine to form a single character, or “grapheme”, and thus shouldn’t be reversed. You have to use something that understands Unicode combining character sequences, like TextElementEnumerator:

// using System.Globalization;

TextElementEnumerator enumerator =
    StringInfo.GetTextElementEnumerator("Les Misאֳrables");

List<string> elements = new List<string>();
while (enumerator.MoveNext())
    elements.Add(enumerator.GetTextElement());

elements.Reverse();
string reversed = string.Concat(elements);  // selbarאֳsiM seL

Leave a Comment