StringBuilder.ToString() 正在添加 '\'字符串开头和结尾的字符
StringBuilder.ToString()
在字符串的开头和结尾添加“\”字符。
这是为什么呢?
在调用 .ToString() 之前,字符串没有“\”字符。
StringBuilder.ToString()
is adding '\' characters in the beginning and ending of the string.
Why is this?
Before calling .ToString() the string doesn't have the '\' character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在这里想到这些反斜杠吗?
如果是这样,则您误读了输出:反斜杠实际上并不在字符串中。它们仅显示在此处,以使表示根据 C# 语法有效。例如,如果您要使用Console.WriteLine 输出字符串,则它不会有反斜杠。
公平地说,这是不一致的。
StringBuilder
的调试视图没有反斜杠,但字符串的调试视图有。Are you thinking of these backslashes here?
If so, you are misreading the output: The backslashes are not actually in the string. They are only displayed here to make the representation valid according to C# syntax. If you were to output the string using
Console.WriteLine
, for example, it would not have backslashes.To be fair, it is inconsistent. The debug view for the
StringBuilder
doesn’t have the backslashes, but the one for strings does.StringBuilder 不会添加除您所拥有的字符之外的任何字符附加。也许您正在查看由字符串生成器创建的字符串的调试视图和字符可能需要转义,并在其前面添加反斜杠 (
\
),以便于复制调试输出。\
字符实际上并不在字符串数据本身中,而是字符串显示的一部分。编辑:这是您可以运行的测试
StringBuilder does not add any characters besides those you have appended. Perhaps you are looking at the Debug View of a string created by string builder and characters which may need to be escaped have the backslash (
\
) in front of them for convenience of copying the debug output.The
\
characters are not actually in the string data itself, but rather part of the display of the string.Edit: here is a test you can run
StringBuilder 中有什么文本?
下面的代码只是为我写出“hello”,
我错过了什么吗?
What text do you have in the StringBuilder?
The following code just writes out "hello" for me
Am I missing something?