简体   繁体   English

类似于 F#(或 C#)中 python 的三重引号?

[英]something like a python's triple-quote in F# (or C#)?

I want to assign a xml code into a string variable.我想将 xml 代码分配给字符串变量。 I can do this without escaping single or double-quotes by using triple-quote in python. Is there a similar way to do this in F# or C#?我可以通过在 python 中使用三引号在没有 escaping 单引号或双引号的情况下执行此操作。在 F# 或 C# 中是否有类似的方法来执行此操作?

F# 3.0 supports triple quoted strings. F#3.0支持三引号字符串。 See Visual Studio F# Team Blog Post on 3.0 features. 请参阅Visual Studio F#Team Blog Post 3.0功能。

The F# 3.0 Spec Strings and Characters section specifically mentions the XML scenario: F#3.0 Spec Strings and Characters部分专门提到了XML场景:

A triple-quoted string is specified by using three quotation marks (""") to ensure that a string that includes one or more escaped strings is interpreted verbatim. For example, a triple-quoted string can be used to embed XML blobs: 通过使用三个引号(“”“)指定三引号字符串,以确保逐字解释包含一个或多个转义字符串的字符串。例如,可以使用三引号字符串来嵌入XML blob:

As far as I know, there is no syntax corresponding to this in C# / F#. 据我所知,C#/ F#中没有与此对应的语法。 If you use @"str" then you have to replace quote with two quotes and if you just use "str" then you need to add backslash. 如果使用@"str"则必须用两个引号替换quote,如果只使用"str"则需要添加反斜杠。

In any case, there is some encoding of " : 在任何情况下,都有一些编码"

var str = @"allows
  multiline, but still need to encode "" as two chars";
var str = "need to use backslahs \" here";

However, the best thing to do when you need to embed large strings (such as XML data) into your application is probably to use .NET resources (or store the data somewhere else, depending on your application). 但是,当您需要将大型字符串(例如XML数据)嵌入到应用程序中时,最好的做法可能是使用.NET资源(或将数据存储在其他位置,具体取决于您的应用程序)。 Embedding large string literals in program is generally not very recommended. 通常不建议在程序中嵌入大型字符串文字。 Also, there used to be a plugin for pasting XML as a tree that constructs XElement objects for C#, but I'm not sure whether it still exists. 此外,曾经有一个插件用于将XML粘贴为为C#构造XElement对象的树,但我不确定它是否仍然存在。

Although, I would personally vote to add """ as known from Python to F# - it is very useful, especially for interactive scripting. 虽然,我个人会投票添加"""从Python到F#已知 - 它非常有用,特别是对于交互式脚本。

In case someone ran into this question when looking for triple quote strings in C# (rather than F#), C#11 now has raw string literals and they're (IMO) better than Python's (due to how indentation is handled)!万一有人在 C#(而不是 F#)中寻找三引号字符串时遇到这个问题,C#11 现在有原始字符串文字,而且它们 (IMO) 比 Python 更好(由于缩进的处理方式)!

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 are not included in the final content:原始字符串文字以至少三个双引号 (""") 字符开头。它以相同数量的双引号字符结尾。通常,原始字符串文字在一行中使用三个双引号来开始字符串,和三个双引号在单独的一行上以结束字符串。开始引号之后和结束引号之前的换行符不包括在最终内容中:

 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. """;

Any whitespace to the left of the closing double quotes will be removed from the string literal.结束双引号左侧的任何空格将从字符串文字中删除。 Raw string literals can be combined with string interpolation to include braces in the output text.原始字符串文字可以与字符串插值相结合,以在 output 文本中包含大括号。 Multiple $ characters denote how many consecutive braces start and end the interpolation:多个$字符表示有多少个连续的大括号开始和结束插值:

 var location = $$""" You are at {{{Longitude}}, {{Latitude}}} """;

The preceding example specifies that two braces starts and end an interpolation.前面的示例指定两个大括号开始和结束插值。 The third repeated opening and closing brace are included in the output string.第三个重复的左括号和右括号包含在 output 字符串中。

https://devblogs.microsoft.com/do.net/csharp-11-preview-updates/#raw-string-literals https://learn.microsoft.com/en-us/do.net/csharp/whats-new/csharp-11 https://devblogs.microsoft.com/do.net/csharp-11-preview-updates/#raw-string-literals https://learn.microsoft.com/en-us/do.net/csharp/whats-新/csharp-11

As shoosh said, you want to use the verbatim string literals in C#, where the string starts with @ and is enclosed in double quotation marks. 正如shoosh所说,你想在C#中使用逐字字符串文字,其中字符串以@开头,并用双引号括起来。 The only exception is if you need to put a double quotation mark in the string, in which case you need to double it 唯一的例外是如果你需要在字符串中加一个双引号,在这种情况下你需要加倍它

System.Console.WriteLine(@"Hello ""big"" world");

would output 会输出

Hello "big" world

http://msdn.microsoft.com/en-us/library/362314fe.aspx http://msdn.microsoft.com/en-us/library/362314fe.aspx

In C# the syntax is @"some string" 在C#中,语法是@"some string"

see here 看这里

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

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