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

前端 未结 2 1821
不知归路
不知归路 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:46

    The simplest way is to use \U######## where the U is capital, and the # denote exactly eight hexadecimal digits. If the value exceeds 0000FFFF hexadecimal, a surrogate pair will be needed:

    string myString = "In the game of mahjong \U0001F01C denotes the Four of circles";
    

    You can check myString.Length to see that the one Unicode character occupies two .NET Char values. Note that the char type has a couple of static methods that will help you determine if a char is a part of a surrogate pair.

    If you use a .NET language that does not have something like the \U######## escape sequence, you can use the method ConvertFromUtf32, for example:

    string fourCircles = char.ConvertFromUtf32(0x1F01C);
    

    Addition: If your C# source file has an encoding that allows all Unicode characters, like UTF-8, you can just put the charater directly in the file (by copy-paste). For example:

    string myString = "In the game of mahjong 

提交回复
热议问题