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#?
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
Related
What is the purpose of triple douple quotes """ in C#? It seems it is used for multiline text. But why not use single double quotes with #"..."?
string text = """
some text
some text
some text
""";
I think a simple example can explain better than many a text.
Suppose we have a sql query which we want to keep in a good format
to be easy readable.
If we put it naive, it won't compile:
string sql =
"select id,
name
from MyTable"; // <- Doesn't compile
We can use # to have verbatim strings which now compiles
string sql =
#"select id,
name
from MyTable";
...
// A little bit different format somewhere else in c# code
string sameSql = #"select id,
name
from MyTable";
But yet another problem arises: we have different strings and that's why
RDBMS will treat them as different queries, both versions will be parsed
and optimized, put into cache etc. So we have the same job done several times (and even worse: parsed queries cache can be flooded by same query in different formats and it'll be not enough space for other queries).
From sql we have
select id,
name
from MyTable
From sameSql we have the same query but in a different format:
select id,
name
from MyTable
Note that leading spaces are preserved (we used verbatim strings, right?) and that's a problem.
The solution is to use new """ construction
string sql =
"""
select id,
name
from MyTable
""";
...
// A little bit different format
string sameSql = """
select id,
name
from MyTable
""";
In both cases we'll get the same text
select id,
name
from MyTable
the query will be parsed, optimized and put into cache just once, c# code style ignored.
Source: C# 11 Preview Updates – Raw string literals
If you work with strings literal that contain quotes or embedded
language strings like JSON, XML, HTML, SQL, Regex and others, raw
literal strings may be your favorite feature of C# 11. Previously if
you copied a literal string with quotes into a C# literal, the string
ended at the first double quote with compiler errors until you escaped
each one. Similarly, if you copied text with curly braces into an
interpolated string literal, each curly bracket was interpreted as the
beginning of a nested code expression unless you escape it, generally
by doubling the curly bracket.
Raw string literals have no escaping. For example, a backslash is
output as a backslash, and \t is output as the backslash and a t,
not as the tab character.
Raw string literals start and end with at least three double quotes
("""..."""). Within these double quotes, single " are considered
content and included in the string. Any number of double quotes less
than the number that opened the raw string literal are treated as
content. So, in the common case of three double quotes opening the raw
string literals, two double quotes appearing together would just be
content. If you need to output a sequence of three or more double
quotes, just open and close the raw string literal with at least one
more quote than that sequence.
Raw string literals can be interpolated by preceding them with a $.
The number of $ that prefixes the string is the number of curly
brackets that are required to indicate a nested code expression. This
means that a $ behaves like the existing string interpolation – a
single set of curly brackets indicate nested code. If a raw string
literal is prefixed with $$, a single curly bracket is treated as
content and it takes two curly brackets to indicate nested code. Just
like with quotes, you can add more $ to allow more curly brackets to
be treated as content. For example:
const int veryCold = -30;
const int comfortable = 20;
string jsonString =
$$"""
{
"TemperatureRanges": {
"Cold": {
"High": {{comfortable}},
"Low": {{veryCold}}
}
}
}
""";
Raw string literals also have new behavior around automatically
determining indentation of the content based on leading whitespace. To
learn more about this and to see more examples on this feature, check
out the docs article Raw String Literals.
P.S. Thanks to Roe and ProgrammingLlama for pointing to this articles.
The following simple regex includes four double-quotes that must be matched. I'm not attempting to come up with a solution for this particular regex but am merely using it as a general example:
\s*"Hello"\s*"world"\s*
The problem I've always encountered when writing C# code that contains regexes that must match double-quotes is the cumbersome syntax I've had to use because string literals in C# are double-quote delimited. I've used the two different techniques below, neither of which I like. Aside from the additional complexity required to butcher the original regex into acceptable C# syntax, converting that syntax back into the original regex for additional development is a real pain. Is there any form that would be equally acceptable to both the regex engine and the C# language parser?
The first hack uses escape characters to escape the backslashes and double quotes that must appear literally in the regex. I view this as the most error prone approach because you get buried in backslashes for more complex regexes:
"\\s*\"Hello\"\\s*\"world\"\\s*"
The second hack breaks the original regex into multiple pieces and concatenates them. Pieces that are string literals and contain regex backslashes are preceded by an # character to cause the backslashes to be taken literally rather than as escape characters. I view this as more verbose but less error prone than the previous approach:
#"\s*" + '"' + "Hello" + '"' + #"\s*" + '"' + "world" + '"' + #"\s*"
#"\s*""Hello""\s*""world""\s*" gives the string \s*"Hello"\s*"world"\s*. Simply double the double quotes in an # prepended string (AKA verbatim string) to display a double quotes
Fiddle
I have discovered that by using the hex escape sequence \x22 to represent a double quote the same regex string can be used unaltered both in my regex development application (RegexBuddy) and in a C# string literal. That is, in my development application
\s*"Hello"\s*"world"\s*
can be represented directly as
\s*\x22Hello\x22\s*\x22world\x22\s*
and in a C# string literal the same regex string it can be represented as
#"\s*\x22Hello\x22\s*\x22world\x22\s*"
The string is still cluttered but at least no changes are required.
In javascript I can write
var s = 'He said "Hello" to me';
or
var s = "don't be sad";
In other words, if I want a double quote in the string, I can declare the string using single quotes. If I want a single quote in the string, I can declare the string using double quotes. This is super handy. Has anyone found a way to do something similar in C#?
The single quotes are easy, c# already works with embedding single quotes. As for double quotes, there seems to be 2 common options.
use backslash escape character
var s = "He said \"Hello\" to me";
use single quotes and replace them with double quotes
var s = "He said 'Hello' to me";
s = s.Replace("'","\"");
I can live with option 2, but better is better.
Javascript and C# are both based on C.
In C ' is used to delimit characters and " is used to delimit strings.
In Javascript there is no character type, so if you want a character you use a single-character string. As such it only needs " but many people used to C-style languages were used to using ' and preferred that for such single-character strings used for characters. This soon led to people developing other styles, especially since the escaping rules would make it convenient in exactly the way you say.
C# does have a character type, as well as a string type, so it kept to the C syntax in this regard.
The feature of Javascript syntax arose due to a feature of its typing system that isn't matched by C#, along with their shared history of borrowing syntax from C. It wouldn't work in C# if the designers had wanted it, without massively complicating the distinction between string and char literals.
You can define your extension method on string to little speedup your work and reduce chance to misstype something.
public static string ApostrophesToQuotes(this string s)
{
return s.Replace('\'', '"');
}
And there is one more way to write quotes in string literal.
var s = #"he said ""Hello"" to me");
But you can't mix them, because apostrophes are for single character literal (2 byte integer in UTF-16) and quotes are for string literals (array of characters).
No, there is no way.
Single quotes ' are char variable delimiter
Double quotes " are string variable delimiter
Check the MS string programing guide for further information.
I decided to just use single quotes and replace them with double quotes.
var s = "He said 'Hello' to me";
s = s.Replace("'","\"");
It is slightly annoying because I need to paste things like this
{"children":[{"domid":"sbsmStatusFilter","datatitle":"SBS Status","dataicon":"img/appicons/rsm4-64x64.png"},{"domid":"sbsMonitoringFilters","datatitle":"Monitoring","dataicon":"img/appicons/monitoring64x64.png"}]}
and then use the text editor find replace function to replace all the double quotes with single quotes, then add the text replace code, and I eventually turn it into this
var text3 = #"{'children':[{'domid':'sbsmStatusFilter','datatitle':'SBS Status','dataicon':'img/appicons/rsm4-64x64.png'},{'domid':'sbsMonitoringFilters','datatitle':'Monitoring','dataicon':'img/appicons/monitoring64x64.png'}]}";
text3 = text3.Replace("'", "\"");
Not too bad I guess. At least the text is fairly easy to read so people can modify it.
I am reading a text file with this structure:
20150218;"C7";"B895";00101;"FTBCCAL16"
I read the line and split like this:
System.IO.StreamReader fichero = new System.IO.StreamReader(ruta, Encoding.Default);
while ((linea = fichero.ReadLine()) != null)
{
// Split by ";"
String[] separador = linea.Split(';');
}
But when I see the content of "linea", I have this:
"20150218";\"C7\";\"B895\";"00101";\"FTBCCAL16\"
As you see, the streamreader add some special character to the output like "" and \. I want to obtain this.
20150218;"C7";"B895";00101;"FTBCCAL16"
Is there a way to obtain this?
Thanks in advance! Regards!
You are watching it in Visual Studio debugger, which just shows you your lines this way. You can write your result into a console or into the file. And you will see normal text without special characters.
StreamReader is not adding or modifying the strings read from the file at all.
If you are viewing the contents of separador in the Visual Studio debugger, it will add an escape sequence to any special characters (for display purposes).
The displayed format matches how you would have to enter them in the code editor if you were creating a string constant.
For example,
However, the real contents of these strings (in memory) are not escaped. They are exactly as you expect them to be in your question.
If you output them or try to manipulate them in code they will have the correct contents.
So, your code is correct. You just have to understand escape sequences and how strings appear in the Visual Studio debugger.
Update:
See this question for an explanation of how to display unquoted strings in the debugger.
Okay here is the quotation from MSDN
At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string #"C:\files.txt" will appear in the watch window as "C:\files.txt".
In your case for " it uses \" (Verbatim string)and this can be visible at debugging time.
Why this happens ?
Double quotation mark " is an escape sequence
Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark (")
So when a string purposefully contains an escape sequence, you need to represent it as a verbatim string. That's what compiler do and that's what you see in debugger
Is there a C# syntax with which I can express strings containing double quotes without having to escape them? I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.
Eg. presently for the following string (simple example)
"No," he said.
I write in C# "\"No,\" he said."
But I'd rather write something like Python '"No," he said.', or Ruby %q{"No," he said.}, so I can copy and paste the contents verbatim to other apps.
I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.
Then it sounds like you probably shouldn't have the strings within source code.
Instead, create text files which are embedded in your assembly, and load them dynamically... or create resource files so you can look up strings by key.
There's no form of string literal in C# which would allow you to express a double-quote as just a single double-quote character in source code.
You could try this but you're still effectively escaping:
string s = #"""No,"" he said.";
Update 2022: C# 11 in Visual Studio 2022 version 17.2 (or later) supports raw string literals between """ https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals
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 aren't included in the final content:
Example (note that StackOverflow doesn't yet highlight correctly)
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.
""";