StringBuilder.ToString() 在字符串的开头和结尾添加 '\' 字符 [英] StringBuilder.ToString() is adding '\' characters in the beginning and ending of the string
问题描述
StringBuilder.ToString()
在字符串的开头和结尾添加 '\' 字符.
StringBuilder.ToString()
is adding '\' characters in the beginning and ending of the string.
这是为什么?
在调用 .ToString() 之前,字符串没有 '\' 字符.
Before calling .ToString() the string doesn't have the '\' character.
推荐答案
你在想这些反斜杠吗?
如果是这样,您就误读了输出:反斜杠实际上不在字符串中.它们仅在此处显示以根据 C# 语法使表示有效.例如,如果您使用 Console.WriteLine
输出字符串,则它不会有反斜杠.
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.
公平地说,它不一致.StringBuilder
的调试视图没有反斜杠,但字符串有.
To be fair, it is inconsistent. The debug view for the StringBuilder
doesn’t have the backslashes, but the one for strings does.
这篇关于StringBuilder.ToString() 在字符串的开头和结尾添加 '\' 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!