Unrecognized escape sequence in C#, translating from vb.NET - c#

I'm attempting to translate a vb function into a c# method. What would the below expression be in C#?
"<\$date\$>"
This causes an unrecognized escape sequence when pasted into a C# project

put a # in front of your string. #"<\$date\$>"
A verbatim string literal consists of an # character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is #"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

Related

Why are slashes doubled in pathname in C# programs? [duplicate]

This question already has answers here:
Full path with double backslash (C#)
(5 answers)
How do I write a backslash (\) in a string?
(6 answers)
Closed 6 years ago.
In the C# program that I am reading, the slashes in the pathnames are doubled, for example:
"C:\\Users\\Tim\\Download"
Why are the slashes doubled in pathnames in C# programs, and is this necessary?
Using strings in C# you need to Escape characters using Escape Sequences
Escape Sequences
Character combinations consisting of a backslash () followed by a
letter or by a combination of digits are called "escape sequences." To
represent a newline character, single quotation mark, or certain other
characters in a character constant, you must use escape sequences. An
escape sequence is regarded as a single character and is therefore
valid as a character constant.
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 ("). The
following table lists the ANSI escape sequences and what they
represent.
Note that the question mark preceded by a backslash (\?)
specifies a literal question mark in cases where the character
sequence would be misinterpreted as a trigraph. See Trigraphs for more
information.
\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
\? Literal question mark
\ ooo ASCII character in octal notation
\x hh ASCII character in hexadecimal notation
\x hhhh Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.
For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".
Slashes are not doubled - they are just escaped, because backslash has special meaning in C# strings. Character combination of backslash followed by some characters is called escape sequence. They are used to represent nonprintable characters, actions like carriage returns and characters which has special meaning like double quotes or backslashes.
Samples of escape sequences :
\n - new line
\t - horizontal tab
\" - double quotes
\\ - backslash
So if you want to have backslash character in your string:
"C:\Users\Tim\Download"
you should either use corresponding escape sequence:
"C:\\Users\\Tim\\Download"
Or you can use verbatim string. In verbatim string escape sequences are not processed
#"C:\Users\Tim\Download"
Further reading: Escape Sequences

Convert Python re.sub to C#

I'm trying to do a conversion from Python to C#
sconvert = re.sub(r"([.$+?{}()\[\]\\])", r"\\\1", sconvert)
I couldn't find a C#.Net equivalent to this function to make it easy.
From the Python Manual
re.sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of
pattern in string by the replacement repl. If the pattern isn’t found,
string is returned unchanged. repl can be a string or a function; if
it is a string, any backslash escapes in it are processed. That is, \n
is converted to a single newline character, \r is converted to a
carriage return, and so forth. Unknown escapes such as \j are left
alone. Backreferences, such as \6, are replaced with the substring
matched by group 6 in the pattern.
You are looking for a Regex.Escape method:
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
The sconvert = re.sub(r"([.$+?{}()\[\]\\])", r"\\\1", sconvert) code escapes the characters specified in the [.$+?{}()\[\]\\] range to match literal characters they denote.
Note that Regex.Escape also escapes spaces. If you do not want that, use your custom replace:
var input = "|^.$+?{}()[]\\-";
var escaped = Regex.Replace(input, #"[|^.$+?{}()\[\]\\-]", "\\$&");
Console.Write(escaped);
// => \|\^\.\$\+\?\{\}\(\)\[\]\\\-
I suggest adding |, - and ^, too. See IDEONE demo

Is it possible to use "Unicode character escape sequences" in verbatim string literals?

I want to put escapted unicode chars in verbatim string literals, but I can't figure out how to do it? (maybe I missing somthing simple or its not possible?)
For example I want to do something like this:
// Invalid
#"\b hello \u200f world"
// Invalid
Where \u200f is interpreted as unicode RTL mark not as string '\\u200f"
I know I can do this this:
"\\b hello \u200f world"
but verbatim string literals are useful when your string contain lots of '\' chars.
No. To quote MSDN:
In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals.

Why do we use # with "\" while trying to replace it with another string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the # in front of a string in C#?
why do we use # to replace \ with another string using string.replace(#"\","$$")
i'm using C# windows application
The # in front of a string literal makes it a verbatim string literal, so the backslash \ does not need to be doubled. You can use "\\" instead of #"\" for the same effect.
Because if you didn't, you'd have to escape \ with \\
# is used to what's called verbatim strings
In C#, you can prefix a string with # to make it verbatim, so you don't need to escape special characters.
#"\"
is identical to
"\\"
The C# Language Specification 2.4.4.5 String literals states:
C# supports two forms of string literals: regular string literals and
verbatim string literals.
A regular string literal consists of zero or more characters enclosed
in double quotes, as in "hello", and may include both simple escape
sequences (such as \t for the tab character), and hexadecimal and
Unicode escape sequences.
A verbatim string literal consists of an # character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is #"hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences, and hexadecimal and Unicode
escape sequences are not processed in verbatim string literals. A
verbatim string literal may span multiple lines.
The verbatim string literal, which uses the # character, makes it a little easier in practicality to escape almost all the characters that you would otherwise have to escape individually with the \ character in a string.
Note: the " char will still require escaping even with the verbatim mode.
So I would use it to save time from having to go through a long string to escape all the necessary characters that needed escaping.
Because the backslash is treated as an escape character and you would get an 'Unrecognised escape sequence' error if you didn't use '#'. Using '#' tells the compiler to ignore escape characters. this may be helpful.

What is the difference between a regular string and a verbatim string?

I have a trial version of ReSharper and it always suggests that I switch regular strings to verbatim strings. What is the difference?
A verbatim string is one that does not need to be escaped, like a filename:
string myFileName = "C:\\myfolder\\myfile.txt";
would be
string myFileName = #"C:\myfolder\myfile.txt";
The # symbol means to read that string literally, and don't interpret control characters otherwise.
This is covered in section 2.4.4.5 of the C# specification:
2.4.4.5 String literals
C# supports two forms of string literals: regular string literals and verbatim string literals.
A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.
A verbatim string literal consists of an # character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is #"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
In other words the only special character in a #"verbatim string literal" is the double-quote character. If you wish to write a verbatim string containing a double-quote you must write two double-quotes. All other characters are interpreted literally.
You can even have literal new lines in a verbatim string literal. In a regular string literal you cannot have literal new lines. Instead you must use for example "\n".
Verbatim strings literals are often useful for embedding filenames and regular expressions in the source code, because backslashes in these types of strings are common and would need to be escaped if a regular string literal were used.
There is no difference at runtime between strings created from regular string literals and strings created from a verbatim string literals - they are both of type System.String.
There is no runtime difference between a string and verbatim string. They're only different at compile time. The compiler accepts fewer escape sequences in a verbatim string so what-you-see-is-what-you-get other than a quote escape.
You can also use the verbatim character, #, to tell the compiler to treat a keyword as a name:
var #if = "if";
//okay, treated as a name
Console.WriteLine(#if);
//compiler err, if without # is a keyword
Console.WriteLine(if);
var #a = "a";
//okay
Console.WriteLine(#a);
//also okay, # isn't part of the name
Console.WriteLine(a);
You can have multiline string too using verbatim strings:
Console.WriteLine(#"This
is
a
Test
for stackoverflow");
without # you got an error.
In VB14 there is a new feature called Multiline Strings, it's like verbatim strings in C#.
Pro tip: VB string literals are now exactly like C# verbatim strings.
Regular strings use special escape sequences to translate to special characters.
/*
This string contains a newline
and a tab and an escaped backslash\
*/
Console.WriteLine("This string contains a newline\nand a tab\tand an escaped backslash\\");
Verbatim strings are interpreted as is, without translating any escape sequences:
/*
This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.
*/
Console.WriteLine(#"This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.");
If you want to suppress the ReSharper warnings, you can use:
Localizable(false)
For things like parameters, file locations, etc., this could be a good solution.

Categories

Resources