Perhaps due to the lack of a built-in String.Reverse method in the .NET
Framework, it’s very common
(1,
2, 3,
4,
5,
6,
7) for implementations
of such a method to be posted.
Unfortunately, most of these implementations do not handle characters outside
Unicode’s Basic Multilingual
Plane correctly. These
supplementary
characters have code
points between U+10000 and U+10FFFF and so cannot be represented with one
16-bit char. In UTF-16 (which is how .NET strings are encoded), these Unicode
characters are represented as two C# chars, a high
surrogate followed by
a low surrogate. When
the string is reversed, the order of these two chars has to be preserved.
Here’s our method that reverses a string while handling surrogate code units
correctly: