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\"]"
Related
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(#"\");
I have to affect special characters to a string variable like \* or """ or ''
I remarked that the comment worked very well because they are contained in "" so they are considered like string.The problem is how i can i force that """ will be considered as string .
my code:
if (line.StartsWith("/*") || line.StartsWith("""""));
or
string a= """""
help me please
You need to escape the quotes:
line.StartsWith("\"\"\"")
The \ acts as an escape character, which prevents the quote from ending the string, and treats it instead as an embedded quotation character.
I have a short question.
I want do put this \tssr>"&\8=f23' into a String (Language C#).
But the compiler always shows an error because for example "\8" is a command.
can someone help me?
Thank you very mutch.
string s = "\\tssr>\"&\\8=f23'";
OR
string s = #"\tssr>""&\8=f23'";
try
string s = #"\tssr>\"&\8=f23";
For double quote[EDIT]
string s = #"\tssr>""&\8=f23";
because \ is special char you need to escape it with either # for \\ as given in below answer
Just write \\8 instead of \8. Or put an # in front of the string. Other characters also need to be escaped with the \ character:
"\\tssr>\"&\\8=f23'"
or this:
#"\tssr>""&\8=f23'"
the backslash \ is used for escaping special characters, like tab or newline. Because of that, the first character also needs to be escaped, because \t is the escape code for Tab.
Try escape sequence
Check this
\\tssr>\"&\\8=f23\'
See below.
var str = "\\tssr>\"&\\8=f23'";
I add a backslash to escape the special characters
The backslash is an escape character in C#, which forms part of an escape sequence.
You have two options: either use TWO backslashes (also known as escaping the backslash) for example var foo = "hello\\world";, or embed the sequence into a string literal eg var foo = #"hello\world";.
Try this:
string s = " \\tssr>\"&\\8=f23' ";
The following works in vb.net, and basically only allows characters on a standard US Keyboard. Any other character pasted gets deleted. I use the following regular expression code:
"[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'""<>,\.|? ]", "")
However when I try to use it in C# it won't work, I used '\' as a escape sequence. C# seems a bit different when it comes to escape sequences? Any help would be appreciated.
Prefix the string with #. That's it. From there you can use the regex string from VB as is (including doubling up on the " character).
// Note: exact same string you're using, only with a # verbatim prefix.
string regex = #"[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'""<>,\.|? ]";
string crazy = "hĀečlĤlŁoźtƢhǣeǮrȡe";
Console.WriteLine(Regex.Replace(crazy, regex, ""));
Output:
hellothere
Prefix your string with "#" and prefix quotes within the string with "\".
I.e. this string
abc\def"hij
in C# would be encoded as
#"abc\def\"hij"
You need to escape your " character. Do this by putting a \ before your " character.
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\/:;'""<>,.|? ]"
should become
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\/:;'\"\"<>,.|? ]"
If you use the #prefix before this, it will treat the backslash literally instead of an escape character and you wont get the desired result.
Escape your characters:
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\\/:;'\"<>,.|? ]"
A good tool for regular expression design and testing (free) is:
http://www.radsoftware.com.au/regexdesigner/
You need to escape you regex for use in C#
[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'\"<>,\.|? ]
Try this one!
While searching on how to escape a single quote in String.Format, I found the answer at SO: Escaping single quote in String.Format()
It seems to be different for VB though. I tested it, and indeed C# needs
string s = DateTime.Now.ToString("MMM d \\'yy 'at' H:mmm");
while VB needs
Dim s As String = Now.ToString("MMM d \'yy 'at' H:mmm")
Why does C# need a double backslash, and VB a single backslash? This might be a bit of a silly question to C# users, but while I can read C#, I'm not used to writing it.
In C#, string literals can contain escape sequences such as \n for a new line or \t for a tab or \" for a quote. If you do not need the escaping, you can prefix the literal with # (eg: #"MMM ...") and get the same string a VB.
In VB, escaping is never allowed, so there is no need to escape the backslash.
The reason why is that C# supports escape sequences within string literals via the \ character. VB has no such escaping mechanisms and hence the single \ is interpretted as a \.
In C# you can get the same behavior by using verbatim strings
#"MMM d \'yy 'at' H:mmm"
In C# the backslash has a meaning (\n is newline \t tab ....).
So backlslahs itselft is an escape character - which you have to escape :)
Or place a AT-sign in front of the string - this makes a "non escaped string" (typically used for paths)
In c# \ will escape . Your text will become "MMM d \'yy 'at' H:mmm". You don't need to escape the ' character in a string. If you were to use " in the string on the other hand, you would need to escape it to not end your string "MMM d \"yy \"at\" H:mmm". Or you could also use the #"" string method which will automatically escape characters for you (not " though). So you could write #"this will not \n be two lines"