简体   繁体   English

如何在C#中的字符串中保留多个引号

[英]How do I maintain multiple quotes quoted in a string in c#

So I have this piece of text that I need to be on a string so I can later add to a text file and should be like this string 所以我有一段文本需要放在字符串上,以便以后可以添加到文本文件中,并且应该像这样

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<requiredRuntime version="v4.0.20506" />
</startup>

I've tried to verbate it like 我试着像

@"""<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <requiredRuntime version="v4.0.20506" />
    </startup>"""

and also tried to work with concatenatio but I can't see to figure out how to include every quote to be on that string. 并且尝试使用串联,但是我看不出如何在字符串中包含每个引号。

Double quotes escape a single quote within @"" : 双引号将@""内的单引号引起来:

string Text = @"<startup useLegacyV2RuntimeActivationPolicy=""true"">
    <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
    <requiredRuntime version=""v4.0.20506"" />
  </startup>";

Handy tool here http://www.freeformatter.com/java-dotnet-escape.html 方便的工具在这里http://www.freeformatter.com/java-dotnet-escape.html

Input the string and it will escape it for you. 输入字符串,它将为您转义。

"<startup useLegacyV2RuntimeActivationPolicy=\"true\">\r\n    <supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\"/>\r\n    <requiredRuntime version=\"v4.0.20506\" />\r\n  </startup>"
private static void Main(string[] args)
        {
            string value =
                @"<startup useLegacyV2RuntimeActivationPolicy=""true""> <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
                 <requiredRuntime version=""v4.0.20506"" /></startup>";
            Console.WriteLine(value);
            Console.Read();
        }

You can preface the initial double-quote of a string with the '@' character to handle escaping it : 您可以在字符串的初始双引号前加上'@'字符来处理转义:

var startupTag = @"<startup useLegacyV2RuntimeActivationPolicy=""true"">
                           <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
                           <requiredRuntime version=""v4.0.20506"" />
                   </startup>";

You can see a live example of this here . 您可以在此处看到一个实时示例

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

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