转义字符的问题
我有一个字符串变量。它包含文本:
\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的想法是要显示反斜杠吗?如果是这样,反斜杠将需要位于原始字符串中。
如果您从字符串文字中获取该文本,则只需将其设为 逐字字符串:
如果想要传入一个确实包含 Unicode“nul”字符 (U+0000) 的字符串,那么您将不会能够让 Windows 显示它。您应该首先删除这些字符:
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:
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:
或者
or
好吧,我不知道您的文本来自哪里,但如果必须的话,您可以使用
但是,如果不够快,字符串将已经包含空字符。
Well, I don't know where your text is coming from, but if you have to, you can use
However, if it's not soon enough, the string will already contain null characters.
不,不是。这就是调试器告诉您它包含的内容。调试器会自动格式化内容,就像您在源代码中将其编写为文字值一样。该字符串实际上并不包含反斜杠,它们是由调试器格式化程序添加的。
该字符串实际上包含二进制零。您可以使用 string.ToCharArray() 亲自查看这一点。您无法按原样显示该字符串,必须去掉零。以十六进制显示内容可以工作,例如 BitConverter.ToString(byte[]) 可以帮助解决这个问题。
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.
你不能。
标准 Windows 控件无法显示空字符。
如果您尝试显示文字文本
\0
,请将字符串更改为以@
符号开头,这会告诉编译器不要解析转义序列。 (@\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n"
)如果你想显示尽可能多的字符串,你可以去掉空值,像这样:
您还可以用转义码替换它们:
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:
You can also replace them with escape codes:
您可以尝试转义
\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.