reading raw string from file in C# - 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.

Related

Is there C# raw string with delimiter like in 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.

using a variable for the format string in String.Format, when it includes \n

I would like to read a line from a file, and use that line as the format specifier for a string.
The line in the file contains a newline character escape sequence \n. so it may look like this:
Hello{0}\n\nWelcome to the programme
And I want to use it in a situation like this:
string name_var = "Steve";
string line = ReadLineFromFile();
string formatted_line = String.Format(line, name_var);
Unfortunately, the result looks like:
Hi Steve\n\nWelcome to the programme
Whereas I'd like it to look like:
Hi Steve
Welcome to the programme
Any ideas?
When you use escape sequences such as \n in your C# program, the compiler processes the string, and inserts the code of the corresponding non-printable character into the string literal for you.
When you read a line from a file or from another import source, processing of escape sequences becomes your program's responsibility. There is no built-in way of doing it. If all you want is \n, you could use a simplistic
formatString = formatString.Replace("\\n", "\n");
If you would like to support other escape sequences, such as ones for UNICODE characters, you could use solutions from answers to this question.
Replace the \n in your string with the newline specifier like this:
string name_var = "Steve";
string line = ReadLineFromFile();
line = line.Replace("\\n", Environment.NewLine);
string formatted_line = String.Format(line, name_var);

String Replace doesn't replace double back slashes in c#

Maybe I am missing something here
I have a variable dir coming in looking something like \\\\SERVERNAME\\dir\\subdir
I need it to look like \\SERVERNAME\dir\subdir
I used string.Replace routine but it did not replace the double slashes, the problem is that when i try to use the path as is, it doesn't find the file.
How would I use string.Replace here in order to get a valid path?
dir.Replace(#"\\", #"\") should do the trick.
In C# the backslash character, "\", is used to escape characters in strings. For example, in the string "Hello\nworld", the "\n" represents a newline character. So, in general, when C# sees a "\" in a string it expects to treat it as part of a special command character, rather than as a literal "\".
So, how do you tell C# that you want a literal backslash to appear in your string, that it isn't part of a special command character? You escape the backslash. And the escape character is also a backslash. So to tell C# that you really want a literal "\" to appear in your string (eg in a file path) you use two backslashes: "\\".
Say I wanted to set a variable to the following path: C:\Temp\FileDrop
In C# I'd have to do the following:
string myPath = "C:\\Temp\\FileDrop";
I suspect that when you see the value of a variable looking like \\\\SERVERNAME\\dir\\subdir it is escaping the backslash characters so the real value of the variable is \\SERVERNAME\dir\subdir.
By the way, if you're copying and pasting long paths from, say, Windows Explorer, it can be a real pain to have to double up the backslashes to escape them. So C# has a special string literal character, "#". If you prefix a string with a "#" then it will treat the string exactly as written. eg
string myPath = #"C:\Temp\FileDrop";

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.

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