转义字符的问题

发布于 2024-09-30 11:08:08 字数 184 浏览 4 评论 0原文

我有一个字符串变量。它包含文本:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

当我尝试将其添加到 TextBox 控件时,没有任何反应。因为 \0 表示 END。

如何按原样添加文本?

更新: 文本是动态放置在变量中的。因此,@ 不适合。

I have a string variable. And it contains the text:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

When I try to add it to the TextBox control, nothing happens.Because \0 mean END.

How do I add text as it is?

UPDATE:
The text is placed in the variable dynamically.Thus, @ is not suitable.

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

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

发布评论

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

评论(6

带上头具痛哭 2024-10-07 11:08:08

您的想法是要显示反斜杠吗?如果是这样,反斜杠将需要位于原始字符串中。

如果您从字符串文字中获取该文本,则只需将其设为 逐字字符串

string text = @"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n";

如果想要传入一个确实包含 Unicode“nul”字符 (U+0000) 的字符串,那么您将不会能够让 Windows 显示它。您应该首先删除这些字符:

textBox.Text = value.Replace("\0", "");

Is the idea that you want to display the backslashes? If so, the backslashes will need to be in the original string.

If you're getting that text from a string literal, it's just a case of making it a verbatim string literal:

string text = @"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n";

If want to pass in a string which really contains the Unicode "nul" character (U+0000) then you won't be able to get Windows to display that. You should remove those characters first:

textBox.Text = value.Replace("\0", "");
笑看君怀她人 2024-10-07 11:08:08
"\\0#«Ия\\0ьw7к\\b\\0E\\0њI\\0\\0ЂЪ\\n"

或者

@"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n"
"\\0#«Ия\\0ьw7к\\b\\0E\\0њI\\0\\0ЂЪ\\n"

or

@"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n"
孤君无依 2024-10-07 11:08:08

好吧,我不知道您的文本来自哪里,但如果必须的话,您可以使用

using System.Text.RegularExpressions;

...

string escapedText = RegEx.Escape(originalText);

但是,如果不够快,字符串将已经包含空字符。

Well, I don't know where your text is coming from, but if you have to, you can use

using System.Text.RegularExpressions;

...

string escapedText = RegEx.Escape(originalText);

However, if it's not soon enough, the string will already contain null characters.

肥爪爪 2024-10-07 11:08:08

它包含文本:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

不,不是。这就是调试器告诉您它包含的内容。调试器会自动格式化内容,就像您在源代码中将其编写为文字值一样。该字符串实际上并不包含反斜杠,它们是由调试器格式化程序添加的。

该字符串实际上包含二进制零。您可以使用 string.ToCharArray() 亲自查看这一点。您无法按原样显示该字符串,必须去掉零。以十六进制显示内容可以工作,例如 BitConverter.ToString(byte[]) 可以帮助解决这个问题。

And it contains the text:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

No it doesn't. That's what the debugger told you it contains. The debugger automatically formatted the content as though you had written it as a literal value in your source code. The string doesn't actually contain the backslashes, they were added by the debugger formatter.

The string actually contains binary zeros. You can see this for yourself by using string.ToCharArray(). You cannot display this string as-is, you have to get rid of the zeros. Displaying the content in hex could work for example, BitConverter.ToString(byte[]) helps with that.

深海夜未眠 2024-10-07 11:08:08

你不能。
标准 Windows 控件无法显示空字符。

如果您尝试显示文字文本 \0,请将字符串更改为以 @ 符号开头,这会告诉编译器不要解析转义序列。 (@\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n")

如果你想显示尽可能多的字符串,你可以去掉空值,像这样:

textBox.Text = someString.Replace("\0", "");

您还可以用转义码替换它们:

textBox.Text = someString.Replace("\0", @"\0");

You can't.
Standard Windows controls cannot display null characters.

If you're trying to display the literal text \0, change the string to start with an @ sign, which tells the compiler not to parse escape sequences. (@\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n")

If you want to display as much of the string as you can, you can strip the nulls, like this:

textBox.Text = someString.Replace("\0", "");

You can also replace them with escape codes:

textBox.Text = someString.Replace("\0", @"\0");
眼眸 2024-10-07 11:08:08

您可以尝试转义 \0 中的反斜杠,即 \\0。请参阅此 MSDN 参考 查看 C# 转义序列的完整列表。

You might try escaping the backslash in \0, i.e. \\0. See this MSDN reference for a full list of C# escape sequences.

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