简体   繁体   English

如何表达包含双引号而没有转义字符的字符串文字?

[英]How to express a string literal containing double quotes without escape characters?

Is there a C# syntax with which I can express strings containing double quotes without having to escape them?是否有一种 C# 语法可以表达包含双引号的字符串而不必转义它们? I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.我经常将 C# 源代码之间的字符串复制并粘贴到其他应用程序,不断添加和删除反斜杠令人沮丧。

Eg.例如。 presently for the following string (simple example)目前用于以下字符串(简单示例)

"No," he said. “不,”他说。

I write in C# "\"No,\" he said."我用 C# 写"\"No,\" he said."

But I'd rather write something like Python '"No," he said.'但我宁愿写 Python '"No," he said.' , or Ruby %q{"No," he said.} , so I can copy and paste the contents verbatim to other apps. , 或 Ruby %q{"No," he said.} ,所以我可以将内容逐字复制并粘贴到其他应用程序中。

I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.我经常将 C# 源代码之间的字符串复制并粘贴到其他应用程序,不断添加和删除反斜杠令人沮丧。

Then it sounds like you probably shouldn't have the strings within source code.那么听起来你可能不应该在源代码中包含这些字符串。

Instead, create text files which are embedded in your assembly, and load them dynamically... or create resource files so you can look up strings by key.相反,创建嵌入在您的程序集中的文本文件,并动态加载它们......或创建资源文件,以便您可以按键查找字符串。

There's no form of string literal in C# which would allow you to express a double-quote as just a single double-quote character in source code. C# 中没有任何形式的字符串文字可以让您将双引号表示为源代码中单个双引号字符。

你可以试试这个,但你仍然有效地逃避:

string s = @"""No,"" he said.";

Update 2022: C# 11 in Visual Studio 2022 version 17.2 (or later) supports raw string literals between """ https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals 2022 年更新:Visual Studio 2022 版本 17.2(或更高版本)中的 C# 11 支持"""之间的原始字符串文字https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw -字符串文字

Raw string literals are a new format for string literals.原始字符串文字是字符串文字的一种新格式。 Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences.原始字符串文字可以包含任意文本,包括空格、换行符、嵌入引号和其他特殊字符,而无需转义序列。 A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string. The newlines following the opening quote and preceding the closing quote aren't included in the final content:原始字符串文字至少以三个双引号 (""") 字符开头。它以相同数量的双引号字符结尾。通常,原始字符串文字在单行上使用三个双引号来开始字符串,和三个双引号在单独的一行结束字符串。开始引号之后和结束引号之前的换行符不包含在最终内容中:

Example (note that StackOverflow doesn't yet highlight correctly)示例(请注意 StackOverflow 尚未正确突出显示)

string longMessage = """
    This is a long message.
    It has several lines.
        Some are indented
                more than others.
    Some should start at the first column.
    Some have "quoted text" in them.
    """;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM