'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. Is there a similar way to do this in F# or C#?



Solution 1:[1]

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:

Solution 2:[2]

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

Solution 3:[3]

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://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11

Solution 4:[4]

In C# the syntax is @"some string"

see here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bentayloruk
Solution 2 Samuel
Solution 3 jack1142
Solution 4 shoosh