在 C# 中为字符串分配双引号的最易读的方法

发布于 2024-08-06 19:12:36 字数 280 浏览 1 评论 0原文

还有其他人认为在非常短的字符串中转义字符会使它们不太可读吗?我注意到我在代码中使用 s = "\"" 为字符串分配双引号,但经过思考,我想出了以下替代方案: s = '" '.ToString()

  • 我的替代方案好吗?您希望看到代码中的第一个版本吗?
  • 如何将两个双引号 ("")(可能是 s = "\"\"")分配给字符串?

/我在被迫进入该 CW 之前正在对其进行标记。

Does anyone else think that escaping characters in very short strings make them not very readable? I noticed I was using s = "\"" in my code to assign a double quote a string, but having thought about it, I came up with the following alternative: s = '"'.ToString().

  • Is my alternative any good? Would you prefer see the first version in code?
  • How would you go about assigning two double quotes (""), which might be s = "\"\"", to a string?

/me is marking this CW before being pressured into it.

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

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

发布评论

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

评论(2

想你的星星会说话 2024-08-13 19:12:36

你可以使用:

String s = new String('"', 1);

或者如果你喜欢迷惑人们:

String s = @"""";

但实际上我仍然更喜欢老式的转义:\"

You could use:

String s = new String('"', 1);

or if you like to confuse people:

String s = @"""";

but actually I still prefer the good-old-fashioned escape: \"

丑疤怪 2024-08-13 19:12:36

我不确定替代方案是否更具可读性,相反它令人困惑。此外,使用函数调用在源代码中获得不同的外观没有多大意义 - 我什至会说这是不好的做法。

恕我直言,老式的转义序列是最好的选择。

I'm not sure the alternative is more readable, on the contrary it's confusing. Besides, using a function call to have a different look in the source code doesn't make much sense - I would even say it's bad practice.

The old-fashioned escape sequence is the best option IMHO.

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