C# 中的 \U 转义序列
我正在尝试转义序列,但不能真正使用 \U 序列(UTF-32) 它无法编译,因为由于某种原因它无法识别序列。 它会将其识别为 UTF-16。
请你帮助我好吗?
Console.WriteLine("\U00HHHHHH");
I am experimenting with the Escape sequences and can not really use the \U sequence (UTF-32)
It does not compile as it can not recognize the sequence for some reason.
It recognizes it as UTF-16.
Could you please help me?
Console.WriteLine("\U00HHHHHH");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您从文档页面复制了
\U00HHHHHH
字符串(C# 编程指南):字符串转义序列:但是
\U00HHHHHH
本身并不是一个有效的 UTF-32 转义序列 - 它是一个掩码,其中每个H
表示位置必须输入 十六进制 字符。它无效的原因是十六进制数字由数字 0-9 和字母 A–F 或 a–f 组成,而H
不是这些字符之一。注释中提到的文字"\U001effff"
不起作用,因为它超出了文档中随后指定的有效 UTF-32 字符值的范围:Your problem is that you copied
\U00HHHHHH
from the documentation page Strings (C# Programming Guide): String Escape Sequences:But
\U00HHHHHH
is not itself a valid UTF-32 escape sequence -- it's a mask where eachH
indicates where a Hex character must be typed. The reason it's not valid is that hexadecimal numbers consist of the digits 0-9 and the letters A–F or a–f -- andH
is not one of these characters. And the literal mentioned in comments,"\U001effff"
, does not work because it falls outside the range the range of valid UTF-32 characters values specified immediately thereafter in the docs:The c# compiler actually checks to see if the specified UTF-32 character is valid according to these rules:
See https://dotnetfiddle.net/KezdTG.
As an aside, to properly display Unicode characters in the Windows console, see How to write Unicode characters to the console?.