Is there C# raw string with delimiter like in C++? - c#

In C++, we have raw literal string using R"()" with delimiter. So this code below is fine:
const char SOMETEXT[] = R"+-+-+(<link rel="icon" href="img/favicon.png">)+-+-+"
I don't know how to do it in C#, as far as i know there is a verbatim string using #"" but it doesn't have delimiter. And this causes error:
string SOMETEXT = #"<link rel="icon" href="img/favicon.png">";
Is there any raw literal string with delimiter in C#? Because i don't want to change the string, it will PITA to edit later.

No, there's nothing like this in C#. If this is arbitrary text that you'll need to edit reasonably frequently, you might want to put it into a resource file instead.
Another alternative I often use for JSON that appears in tests etc is to just use single quotes instead of double quotes, then replace afterwards:
string text = "<link rel='icon' href='img/favicon.png'>".Replace('\'', '"');

In an #ed string (aka a verbatim string literal) you can use "" for double qoutes.
string SOMETEXT = #"<link rel=""icon"" href=""img/favicon.png"">";
In a regular string literal you can use a backslash.
string someText2 = "<link rel=\"icon\" href=\"img/favicon.png\">";
More on differences between verbatin and standard string literals
Please note that only the quote escape sequence ("") in not interpreted literally in a verbatim literal string; all others are taken literally.
Here is an example from # (C# Reference).
The following example illustrates the effect of defining a regular string literal and a verbatim string literal that contain identical character sequences.
string s1 = "He said, \"This is the last \u0063hance\x0021\"";
string s2 = #"He said, ""This is the last \u0063hance\x0021""";
Console.WriteLine(s1);
Console.WriteLine(s2);
// The example displays the following output:
// He said, "This is the last chance!"
// He said, "This is the last \u0063hance\x0021"
I don't want to change the string!
If you don't want to change the string you can embed it as a file. For unit testing I often store json files as embedded resources and load them with GetManifestResourceStream.

Related

Escape double quotes in a string

Double quotes can be escaped like this:
string test = #"He said to me, ""Hello World"". How are you?";
But this involves adding character " to the string. Is there a C# function or other method to escape double quotes so that no changing in string is required?
No.
Either use verbatim string literals as you have, or escape the " using backslash.
string test = "He said to me, \"Hello World\" . How are you?";
The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.
You can use backslash either way:
string str = "He said to me, \"Hello World\". How are you?";
It prints:
He said to me, "Hello World". How are you?
which is exactly the same that is printed with:
string str = #"He said to me, ""Hello World"". How are you?";
Here is a DEMO.
" is still part of your string.
You can check Jon Skeet's Strings in C# and .NET article for more information.
In C# you can use the backslash to put special characters to your string.
For example, to put ", you need to write \".
There are a lot of characters that you write using the backslash:
Backslash with other characters
\0 nul character
\a Bell (alert)
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\" Double quotation mark
\\ Backslash
Any character substitution by numbers:
\xh to \xhhhh, or \uhhhh - Unicode character in hexadecimal notation (\x has variable digits, \u has 4 digits)
\Uhhhhhhhh - Unicode surrogate pair (8 hex digits, 2 characters)
Another thing worth mentioning from C# 6 is interpolated strings can be used along with #.
Example:
string helloWorld = #"""Hello World""";
string test = $"He said to me, {helloWorld}. How are you?";
Or
string helloWorld = "Hello World";
string test = $#"He said to me, ""{helloWorld}"". How are you?";
Check running code here!
View the reference to interpolation here!
You're misunderstanding escaping.
The extra " characters are part of the string literal; they are interpreted by the compiler as a single ".
The actual value of your string is still He said to me, "Hello World". How are you?, as you'll see if you print it at runtime.
2022 UPDATE: Previously the answer would have been "no". However, C#11 introduces a new feature called "raw string literals." To quote the Microsoft documentation:
Beginning with C# 11, you can use raw string literals to more easily create strings that are multi-line, or use any characters requiring escape sequences. Raw string literals remove the need to ever use escape sequences. You can write the string, including whitespace formatting, how you want it to appear in output."
SOURCE: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals
EXAMPLE: So using the original example, you could do this (note that raw string literals always begin with three or more quotation marks):
string testSingleLine = """He said to me, "Hello World". How are you?""";
string testMultiLine = """
He said to me, "Hello World". How are you?
""";
Please explain your problem. You say:
But this involves adding character " to the string.
What problem is that? You can't type string foo = "Foo"bar"";, because that'll invoke a compile error. As for the adding part, in string size terms that is not true:
#"""".Length == 1
"\"".Length == 1
In C# 11.0 preview you can use 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.
string test = """He said to me, "Hello World" . How are you?""";
In C#, there are at least four ways to embed a quote within a string:
Escape quote with a backslash
Precede string with # and use double quotes
Use the corresponding ASCII character
Use the hexadecimal Unicode character
Please refer this document for detailed explanation.

reading raw string from file in C#

I have the following raw-string literal : #"some text ""here"" and some more text"
Everything works fine when I have this assigned to a string variable in a program. But when I put this string - "some text ""here"" and some more text" and read it, it is not being recognized as a raw string.
What should I do to have C# recognize this as a raw-string? Specifically, what is the programmatic equivalent of the '#' specifier used to indicate that the string is a raw-string?
why dont you escape it ?
"some text \"here\" and some more text"
There's no programmatic equivalent of the # specifier. The specifier is used only at compile time to convert a verbatim string literal to its internal representation. (BTW, the proper name is verbatim string, not raw-string).
Therefore, str1 and str2 below represent exactly the same string at runtime:
string str1 = "some text \"here\" and some more text";
string str2 = #"some text ""here"" and some more text";
The one and only difference between them is visible in your source code only. You don't need to apply any kind of programmatic transformation when your read strings from a text file.
I think when you say raw string, you mean literal string.
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
Literal strings using the # symbol are just telling the compiler to literally take the text provided.
As far as I know there is no way to convert a string into the literal version of that string.
But you could easily write that code that replaces double quotes with single quotes.

What's the meaning of "#" in C#

new learner's quick question,
what's the meaning of "#" in C# codes?
Examples:
ClientDataSource.Where = #"it.ClientID==1";
cont.Notes = #"";
Response.Redirect(#"~/Default.aspx");
Thanks!
That is a verbatim string literal.
MSDN describes it as such:
Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string.
# can also be used to create identifiers that match reserved words: 2.4.2 Identifiers
For example:
var class = "Reading"; // compiler error
var #class = "Math"; // works
#"...." denotes a verbatim string literal. C# does not process any escape characters in the string, except for "" (to allow including of the " character in the string).
This makes it easier and cleaner to handle strings that would otherwise need to have a bunch of escapes to deal with properly. File/folders paths, for example.
string filePathRegular = "C:\\Windows\\foo\\bar.txt";
string filePathVerbatim = #"C:\Windows\foo\bar.txt";
It's also very useful in writing Regular Expressions, and probably many other things.
It's worth noting that C# also uses the # character as a prefix to allow reserved words to be used as identifiers. For example, Html Helpers in ASP.Net MVC can take an anonymous object containing HTML attributes for the tags they create. So you might see code like this:
<%= Html.LabelFor(m => m.Foo, new { #class = "some-css-class" } ) %>
The # is needed here because class is otherwise a reserved word.
The verbatim string literal allows you to put text inside of a string that would otherwise be treated differently by the compiler. For example, if I were going to write a file path and assign it to a variable, I might do something like so:
myString = "C:\\Temp\\Test.txt";
The reason I have to have the double slashes is because I am escaping out the slash so it isn't treated as an command. If I use the verbatim string literal symbol, my code could look as follows:
myString = #"C:\Temp\Test.txt";
It makes it easier to write strings when you are dealing with special characters.

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

What does "#" mean in C# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
when to use # in c# ?
F.e. string sqlSelect = #"SELECT * FROM Sales".
It means interpret the following string as literal. Meaning, the \ in the string will actually be a "\" in the output, rather than having to put "\\" to mean the literal character
Before string it allows different string formating rules. You can't use backslash to specify special symbols and "" (double quotes become quotes). I find this format very useful for regular expressions
Example
Console.WriteLine(#"\n""\/a"); // outputs \n"\/a
Console.WriteLine("\\n\"\"\\/a"); // outputs \n"\/a
You might also seen # symbol before variable. In such case it allows using special C# keywords as variables.
Example:
var #switch = 1;
var #if = "test";
It means there is no need to escape characters in such a string.
So if you want to write the path for c:\Windows, you can write it as
string path = "c:\\Windows"; // Note escaped '\'
OR
string path = #"c:\Windows"; // '\' need not be escaped
There are two types of string literals, regular and verbatim. The # symbol makes it a verbatim string literal.
MSDN: String literals (C#)
In C and C++, string has some special characters called "escape characters". For example \, & and the " itself is an escape character!
In the very normal way, you to print a statement like:
Nancy Said Hello World! & smiled
you had to set your string like next
string str = "Nancy said Hello World! \& smiled.";
But people in Microsoft made a new cool feature in C# compiler so you can escape the headache of handling the escape characters by adding # before any string, and the compiler will handle all the escape characters for you by himself. For the last example you can have this in C# like next:
string str = #"Nancy said Hello World! & smiled.";
Verbatim string literals start with #
and are also enclosed in double
quotation marks. For example:
#"good morning" // a string literal
Nicked from, have a look at the last few lines above the example for more information.
http://msdn.microsoft.com/en-us/library/362314fe.aspx
Used for string literal. It marks the string within the quote (") marks as the value without applying any interpretation for symbols in that string.
It allows you to have a string with a \ delimiter in it. #"C:\A\b\c\d\e\f" is legal.

Categories

Resources