如何在字符串中包含引号
我有一个字符串“我想学习“c#””。如何在 C# 前后添加引号?
I have a string "I want to learn "c#"". How can I include the quotes before and after c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个字符串“我想学习“c#””。如何在 C# 前后添加引号?
I have a string "I want to learn "c#"". How can I include the quotes before and after c#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
用反斜杠转义它们。
Escape them with backslashes.
除了用反斜杠转义引号外,还请参阅SO问题 2911073 解释了如何在 @ 前缀字符串中使用双引号:
这会导致:
As well as escaping quotes with backslashes, also see SO question 2911073 which explains how you could alternatively use double-quoting in a @-prefixed string:
which results in:
我使用:
与等效的相反,
因为前者的噪音比后者少得多,因此更容易看到拼写错误等。
我在单元测试中经常使用它。
I use:
as opposed to the equivalent
Because the former has far less noise than the latter, making it easier to see typo's etc.
I use it a lot in unit tests.
输出 - “嗨,”我是程序员
OUTPUT - "Hi, " I am programmer
从 .NET 7 开始,您可以使用原始字符串文字,它允许声明字符串而无需转义符号:
如果字符串不以双引号开头或结尾,您甚至可以将其设置为单行:
Since .NET 7 you can use raw string literals, which allows to declare strings without escaping symbols:
If string does not start or end with double quote you can even make it single line:
使用转义字符,例如此代码:
将输出:
Use escape characters for example this code:
will output:
您还可以声明一个常量并每次使用它。整洁并避免混乱:
You can also declare a constant and use it each time. neat and avoids confusion:
代码:
string myString = "Hello " + ((char)34) + " World." + ((char)34);
输出将是:
The Code:
string myString = "Hello " + ((char)34) + " World." + ((char)34);
Output will be: