无法识别带有 unicode 字符的转义序列的问题

发布于 2024-12-07 23:14:45 字数 410 浏览 0 评论 0原文

我正在写一个程序。该程序的一部分涉及从 XML 文件读取 unicode 值并在屏幕上显示该字符。

现在,当我这样做时:

tbTester.Text = "\u597D";

它工作得很好(tbTester 是一个 Winforms 文本框)。但对于其他情况,基本上我需要更改字符串以具有“\u”,然后是值。就像这样:

szOut = szOut + "\u"+k.UnicodeID + " ";

对我来说,这些看起来并没有什么不同。直到现在它才告诉我“\u”是一个无法识别的转义序列。

现在我确实查找了这个问题,双斜杠或“@”符号确实解决了这种特殊情况,只是现在文本框包含“\u430B”(或其他内容)而不是我的第一个示例中输出的字符。

I'm writing a program. Part of this program involves reading in a unicode value from an XML file and displaying the character on screen.

Now when I did it like this:

tbTester.Text = "\u597D";

It worked fine (tbTester is a Winforms text box). But with the other situation, basically I needed to alter a string to have the '\u' and then the value. Like this:

szOut = szOut + "\u"+k.UnicodeID + " ";

For me, these don't look all that different. Only now it tells me that the "\u" is an unrecognised escape sequence.

Now I did look this problem up and a double slash or the '@' symbol does cure this particular situation, only now the text box contains '\u430B' (or whatever) rather than the character which was output in the first of my examples.

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

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

发布评论

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

评论(2

不必你懂 2024-12-14 23:14:45

当您像第一个示例一样编译代码时,编译后的 CIL 代码实际上并不包含转义序列,而是包含字符本身。并且由于 \u 本身无效,这会导致您收到错误。

如果您有整数形式的 Unicode 代码点,则只需通过转换即可将其转换为字符。 + 运算符将处理其余的事情:

szOut = szOut + (char)k.UnicodeID + " ";

尽管在这种情况下我倾向于使用 string.Format()

szOut = string.Format("{0}{1} ", szOut, (char)k.UnicodeID);

When you compile code like your first example, the compiled CIL code doesn't actually contain the escape sequence, but the character itself. And because \u is invalid by itself, this causes the error you're getting.

If you have have Unicode code point as an integer, you can convert it into a character simply by casting. And the + operator will take care of the rest:

szOut = szOut + (char)k.UnicodeID + " ";

Although I tend to prefer string.Format() in cases like this:

szOut = string.Format("{0}{1} ", szOut, (char)k.UnicodeID);
叹沉浮 2024-12-14 23:14:45

尝试

szOut = szOut + (char)k.UnicodeID;

您不再需要转义该字符,因为它不在文字字符串中。您只需将数字转换为其等效字符即可。

Try

szOut = szOut + (char)k.UnicodeID;

You don't need to escape the character anymore since it's not in a literal string. You just need to convert the number to it's character equivelant.

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