Split string using backslash - c#

I want to split a string using the backslash ('\'). However, it's not allowed - the compiler says "newline in constant". Is there a way to split using backslash?
//For example...
String[] breakApart = sentence.Split('\'); //this gives an error.

Try using the escaped character '\\' instead of '\':
String[] breakApart = sentence.Split('\\');
The backslash \ in C# is used as an escape character for special characters like quotes and apostrophes. So when you are trying to wrap the backslash with apostrophes, the backslash together with the final apostrophe is being interpreted as an escaped apostrophe.
Here is a list of character escapes available in C#.
Here is Microsoft's documentation for character literals in C#.

It's backslash, a character literal.
To do the split:
String[] breakApart = sentence.Split('\\');

you can use #
String[] breakApart = sentence.Split(#"\");

Related

how to validate regular expression using System.Text.RegularExpressions.Regex.IsMatch in C# [duplicate]

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.

Why does attempting to verbatimize this string fail?

I have this substring I want to strip out of a string:
<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">
Realizing it was full of funkiness, I thought verbatimizing it would solve all ills:
String messedUpJunk = #"<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">";
...but, to paraphrase the robot on Lost In Space, that does not compute (compile); I get, "; expected" on the first "http".
I can get it compilable by escaping the quotes:
String messedUpJunk = "<ArrayOfSiteQuery xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS\">";
...but what use is verbatim if it's not verbatimatic?
A double quote is the only character you need to escape in verbatim strings. Escaping it is done differently, you need to double it ("") instead of using the backslash:
String messedUpJunk = #"<ArrayOfSiteQuery xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS"">";
MSDN link:
Use double quotation marks to embed a quotation mark inside a verbatim string
It is the double quote which requires escaping (using double double quotes), for rest of the characters you don't need to escape, for example back slash \.
See: 2.4.4.5 String literals - C#
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 reason you need to escape double quote is because it represents start and end of string, whether verbatim or regular.

C#, escape double quote not working as expected

I'm trying to escape quotes in an xpath string like so:
var mktCapNode = htmlDoc.DocumentNode.SelectSingleNode("//*[#id=""yfs_j10_a""]");
The actual string I want passed is:
//*[#id="yfs_j10_a"]
This gives a compiler errors: ) expected and ; expected
I'm sure it's simple but I'm stumped. Any ideas?
You need to make this a verbatim string to use the "" as an escape
#"//*[#id=""yfs_j10_a""]"
For a normal string literal you need to use backslashes to escape the double quotes
"//*[#id=\"yfs_j10_a\"]"
Or use the escape char '\':
"//*[#id=\"yfs_j10_a\"]"
In C# the \ character is used to escape (see documentation).
This is different from VB where there are no escape characters except "" which escapes to ".
This means in C# you do not need vbCrLf to start a new line or vbTab to add a tab character to a string. Instead use "\r\n" and "\t".
You can also make the string a literal using the # character, but I do not think this works with the quotation mark.
Add the # prefix to your string.
#"//*[#id=""yfs_j10_a""]"
or escape the quotes with a \
"//*[#id=\"yfs_j10_a\"]"

How do I create a regular expression to disallow backslash

I am having trouble creating a regular expression to disallow the following four characters and limit the size:
/
#
?
\
What I currently have is:
Regex regex = new Regex("^[^/\\#?]{0,1024}$", RegexOptions.Compiled);
if (!regex.IsMatch("\\"))
{
Console.WriteLine("Bad");
}
All of the characters except \ are disallowed. I cannot get \ to work.
Any suggestions on how to support this?
Your regex is fine, ^[^/\\#?]{0,1024}$.
However, in C# backslash is an escape character, so a C# "\\" is a single backslash.
Hence for each backslash in your regex, you have to backslash again for C#:
Regex regex = new Regex("^[^/\\\\#?]{0,1024}$", RegexOptions.Compiled);
Alternatively, you can use a raw string, meaning backslashes in C# strings remain backslashes (note the # symbol):
Regex regex = new Regex(#"^[^/\\#?]{0,1024}$", RegexOptions.Compiled);
You were close, you need to escape the backslash:
^[^/\\#?]{0,1024}$
Even though you do not need to escape special characters inside a character class you do need to escape the escape character itself.
Try two forward slashes.
^[^/\\#?]{0,1024}$
In C++, the forward slash is reserved for escape characters, like \n. To make a literal forward slash, use \\.

What does the # prefix do on string literals in C#

I read some C# article to combine a path using Path.Combine(part1,part2).
It uses the following:
string part1 = #"c:\temp";
string part2 = #"assembly.txt";
May I know what is the use of # in part1 and part2?
# is not related to any method.
It means that you don't need to escape special characters in the string following to the symbol:
#"c:\temp"
is equal to
"c:\\temp"
Such string is called 'verbatim' or #-quoted. See MSDN.
As other have said its one way so that you don't need to escape special characters and very useful in specifying file paths.
string s1 =#"C:\MyFolder\Blue.jpg";
One more usage is when you have large strings and want it to be displayed across multiple lines rather than a long one.
string s2 =#"This could be very large string something like a Select query
which you would want to be shown spanning across multiple lines
rather than scrolling to the right and see what it all reads up";
As stated in C# Language Specification 4.0:
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.
It denotes a verbatim string literal, and allows you to use certain characters that normally have special meaning, for example \, which is normally an escape character, and new lines. For this reason it's very useful when dealing with Windows paths.
Without using #, the first line of your example would have to be:
string part1 = "c:\\temp";
More information here.
With # you dont have to escape special characters.
So you would have to write "c:\\temp" without #
If more presise it is called 'verbatim' strings. You could read here about it:
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
The # just indicates a different way of specifying a string such that you do not have to escape characters with . the only caveat is that double quotes need to be "" to represent a single ".

Categories