How do I create a string with a surrogate pair inside of it?

前端 未结 2 1822
不知归路
不知归路 2021-02-07 12:04

I saw this post on Jon Skeet\'s blog where he talks about string reversing. I wanted to try the example he showed myself, but it seems to work... which leads me to believe that

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 12:34

    The term "surrogate pair" refers to a means of encoding Unicode characters with high code-points in the UTF-16 encoding scheme (see this page for more information);

    In the Unicode character encoding, characters are mapped to values between 0x000000 and 0x10FFFF. Internally, a UTF-16 encoding scheme is used to store strings of Unicode text in which two-byte (16-bit) code sequences are considered. Since two bytes can only contain the range of characters from 0x0000 to 0xFFFF, some additional complexity is used to store values above this range (0x010000 to 0x10FFFF).

    This is done using pairs of code points known as surrogates. The surrogate characters are classified in two distinct ranges known as low surrogates and high surrogates, depending on whether they are allowed at the start or the end of the two-code sequence.

    Try this yourself:

    String surrogate = "abc" + Char.ConvertFromUtf32(Int32.Parse("2A601", NumberStyles.HexNumber)) + "def";
    
    Char[] surrogateArray = surrogate.ToCharArray();
    Array.Reverse(surrogateArray);
    
    String surrogateReversed = new String(surrogateArray);
    

    or this, if you want to stick with the blog example:

    String surrogate = "Les Mise" + Char.ConvertFromUtf32(Int32.Parse("0301", NumberStyles.HexNumber)) + "rables";
    
    Char[] surrogateArray = surrogate.ToCharArray();
    Array.Reverse(surrogateArray);
    
    String surrogateReversed = new String(surrogateArray);
    

    nnd then check the string values with the debugger. Jon Skeet is damn right... strings and dates seem easy but they are absolutely NOT.

提交回复
热议问题