Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

string

c#

f#

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#?

like image 568
tk. Avatar asked May 15 '10 21:05

tk.


People also ask

Can you use F string with triple quotes?

We can use any quotation marks {single or double or triple} in the f-string. We have to use the escape character to print quotation marks. The f-string expression doesn't allow us to use the backslash. We have to place it outside the { }.

How do you write 3 quotes in Python?

Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code. Special characters like TABs, verbatim or NEWLINEs can also be used within the triple quotes. As the name suggests its syntax consists of three consecutive single or double-quotes.

How do you do a triple double quote in Python?

To create strings that span multiple lines, triple single quotes ''' or triple double quotes """ are used to enclose the string. ''' This string is on multiple lines within three single quotes on either side. ''' """ This string is on multiple lines within three double quotes on either side.


2 Answers

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:

like image 116
bentayloruk Avatar answered Oct 14 '22 18:10

bentayloruk


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.

like image 30
Tomas Petricek Avatar answered Oct 14 '22 20:10

Tomas Petricek