C# 中的 \U 转义序列

发布于 2025-01-15 10:50:27 字数 430 浏览 6 评论 0原文

我正在尝试转义序列,但不能真正使用 \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");

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2025-01-22 10:50:27

您的问题是您从文档页面复制了 \U00HHHHHH 字符串(C# 编程指南):字符串转义序列:

在此处输入图像描述

但是 \U00HHHHHH 本身并不是一个有效的 UTF-32 转义序列 - 它是一个掩码,其中每个 H 表示位置必须输入 十六进制 字符。它无效的原因是十六进制数字由数字 0-9 和字母 A–F 或 a–f 组成,而 H 不是这些字符之一。注释中提到的文字 "\U001effff" 不起作用,因为它超出了文档中随后指定的有效 UTF-32 字符值的范围:

(范围:000000 - 10FFFF;示例:\U0001F47D = "

Your problem is that you copied \U00HHHHHH from the documentation page Strings (C# Programming Guide): String Escape Sequences:

enter image description here

But \U00HHHHHH is not itself a valid UTF-32 escape sequence -- it's a mask where each H 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 -- and H 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:

(range: 000000 - 10FFFF; example: \U0001F47D = "????")*

The c# compiler actually checks to see if the specified UTF-32 character is valid according to these rules:

// These compile because they're valid Hex numbers in the range 000000 - 10FFFF padded to 8 digits with leading zeros:
Console.WriteLine("\U0001F47D");
Console.WriteLine("\U00000000");
Console.WriteLine("\U0010FFFF");
// But these don't.
// H is not a valid Hex character:
// Compilation error (line 16, col 22): Unrecognized escape sequence
Console.WriteLine("\U00HHHHHH");
// This is outside the range of 000000 - 10FFFF:
// Compilation error (line 19, col 22): Unrecognized escape sequence
Console.WriteLine("\U001effff");

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?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文