开发者

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

I want to assign a xml code into a string variable. I can do this without escaping single or double-quotes by using triple-quote in python开发者_JAVA技巧. Is there a similar way to do this in F# or C#?


F# 3.0 supports triple quoted strings. See Visual Studio F# Team Blog Post on 3.0 features.

The F# 3.0 Spec Strings and Characters section specifically mentions the XML scenario:

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:


As far as I know, there is no syntax corresponding to this in 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.

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). 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.

Although, I would personally vote to add """ as known from Python to F# - it is very useful, especially for interactive scripting.


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)!

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

https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/#raw-string-literals https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/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. 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


In C# the syntax is @"some string"

see here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜