StringBuilder.ToString() 在字符串的开头和结尾添加 '\' 字符 [英] StringBuilder.ToString() is adding '\' characters in the beginning and ending of the string

查看:53
本文介绍了StringBuilder.ToString() 在字符串的开头和结尾添加 '\' 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆