如何在字符串中包含引号

发布于 2024-09-14 01:21:30 字数 39 浏览 8 评论 0原文

我有一个字符串“我想学习“c#””。如何在 C# 前后添加引号?

I have a string "I want to learn "c#"". How can I include the quotes before and after c#?

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

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

发布评论

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

评论(8

等待圉鍢 2024-09-21 01:21:30

用反斜杠转义它们。

"I want to learn \"C#\""

Escape them with backslashes.

"I want to learn \"C#\""
嘿咻 2024-09-21 01:21:30

除了用反斜杠转义引号外,还请参阅SO问题 2911073 解释了如何在 @ 前缀字符串中使用双引号:

string msg = @"I want to learn ""c#""";

这会导致:

我想学习“c#”

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:

string msg = @"I want to learn ""c#""";

which results in:

I want to learn "c#"

感性 2024-09-21 01:21:30

我使用:

var value = "'Field1','Field2','Field3'".Replace("'", "\""); 

与等效的相反,

var value = "\"Field1\",\"Field2\",\"Field3\"";

因为前者的噪音比后者少得多,因此更容易看到拼写错误等。

我在单元测试中经常使用它。

I use:

var value = "'Field1','Field2','Field3'".Replace("'", "\""); 

as opposed to the equivalent

var value = "\"Field1\",\"Field2\",\"Field3\"";

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.

南风几经秋 2024-09-21 01:21:30
string str = @"""Hi, "" I am programmer";

输出 - “嗨,”我是程序员

string str = @"""Hi, "" I am programmer";

OUTPUT - "Hi, " I am programmer

递刀给你 2024-09-21 01:21:30

从 .NET 7 开始,您可以使用原始字符串文字,它允许声明字符串而无需转义符号:

string text = """
I want to learn "C#"
""";
Console.WriteLine(text); // Prints string 'I want to learn "C#"'

如果字符串不以双引号开头或结尾,您甚至可以将其设置为单行:

string text = """I want to learn "C#"!""";

Since .NET 7 you can use raw string literals, which allows to declare strings without escaping symbols:

string text = """
I want to learn "C#"
""";
Console.WriteLine(text); // Prints string 'I want to learn "C#"'

If string does not start or end with double quote you can even make it single line:

string text = """I want to learn "C#"!""";
情丝乱 2024-09-21 01:21:30

使用转义字符,例如此代码:

var message = "I want to learn \"c#\"";
Console.WriteLine(message);

将输出:

我想学习“c#”

Use escape characters for example this code:

var message = "I want to learn \"c#\"";
Console.WriteLine(message);

will output:

I want to learn "c#"

勿忘心安 2024-09-21 01:21:30

您还可以声明一个常量并每次使用它。整洁并避免混乱:

const string myStrQuote = "\"";

You can also declare a constant and use it each time. neat and avoids confusion:

const string myStrQuote = "\"";
合久必婚 2024-09-21 01:21:30

代码:

string myString = "Hello " + ((char)34) + " World." + ((char)34);

输出将是:

你好“世界”。

The Code:

string myString = "Hello " + ((char)34) + " World." + ((char)34);

Output will be:

Hello "World."

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