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.
Related
I have stringBuilder & string class, storing a path:
StringBuilder name = new StringBuilder();
name.Append(#"NETWORK\MyComputer");
String serverName = name.ToString(); //this converts the \ to a \\
I have tried a number of things, but it always results in the string having \
Using serverName.Replace("\\", #"\"); doesn't work, it leaves it as a \
servername.Replace("\\", "\""); adds a " to the string, which is still not correct.
Please assist.
If you are concerned at a single back slash being shown as a double back slash then don't be - that is simply the way it is shown to you in the debugger.
The back slash is a special character, that 'specialness' is turned off by doubling it up. Alternatively the # symbol can be prefixed to the string in source code which avoids having to use it.
Use
name.Append(Path.Combine("NETWORK", "MyComputer");
In strings \ is an escape sequence. So \ in debugger will be \\
Acc.to MSDN
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.
Read Escape Sequences
I don't think your code can be compiled. Because \ is an escape character, thus the string "\" will be wrong. The #"\" is right because the # (literal) has ignored that escape and tread it as a normal character.
See more here
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\"]"
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' ";
I've noticed that C# adds additional slashes (\) to paths. Consider the path C:\Test. When I inspect the string with this path in the text visualiser, the actual string is C:\\Test.
Why is this? It confuses me, as sometimes I may want to split the path up (using string.Split()), but have to wonder which string to use (one or two slashes).
The \\ is used because the \ is an escape character and is need to represent the a single \.
So it is saying treat the first \ as an escape character and then the second \ is taken as the actual value. If not the next character after the first \ would be parsed as an escaped character.
Here is a list of available escape characters:
\' - single quote, needed for character literals
\" - double quote, needed for string literals
\\ - backslash
\0 – Null
\a - Alert
\b - Backspace
\f - Form feed
\n - New line
\r - Carriage return
\t - Horizontal tab
\v - Vertical quote
\u - Unicode escape sequence for character
\U - Unicode escape sequence for surrogate pairs.
\x - Unicode escape sequence similar to "\u" except with variable length.
EDIT: To answer your question regarding Split, it should be no issue. Use Split as you would normally. The \\ will be treated as only the one character of \.
.Net is not adding anything to your string here. What your seeing is an effect of how the debugger chooses to display strings. C# strings can be represented in 2 forms
Verbatim Strings: Prefixed with an # sign and removes the need o escape \\ characters
Normal Strings: Standard C style strings where \\ characters need to escape themselves
The debugger will display a string literal as a normal string vs. a verbatim string. It's just an issue of display though, it doesn't affect it's underlying value.
Debugger visualizers display strings in the form in which they would appear in C# code. Since \ is used to escape characters in non-verbatum C# strings, \\ is the correct escaped form.
Okay, so the answers above are not wholly correct. As such I am adding my findings for the next person who reads this post.
You cannot split a string using any of the chars in the table above if you are reading said string(s) from an external source.
i.e,
string[] splitStrings = File.ReadAllText([path]).Split((char)7);
will not split by those chars. However internally created strings work fine.
i.e.,
string[] splitStrings = "hello\agoodbye".Split((char)7);
This may not hold true for other methods of reading text from a file. I am unsure as I have not tested with other methods. With that in mind, it is probably best not to use those chars for delimiting strings!
string aniPattern=#"(?si:<option value=\\\"(?<year>.*?)\\)";
This breakes because the " in the middle. But I need that because I use it in a regex.
I tried to use string aniPattern="(?si:<option value=\\\"(?<year>.*?)\\\\)";(without #) but it isnot a valid regex.
important - it isn't entirely clear what you want to match; I've answered on the premise that only the " is being a problem - but see also Mike Caron's answer which assumes everything is escaped incorrectly.
With a verbatim string literal (i.e. #"..."), " is escaped to "" - so your string becomes:
string aniPattern=#"(?si:<option value=\\\""(?<year>.*?)\\)";
With a regular string literal (without the leading #), you would need a lot worse:
string aniPattern="(?si:<option value=\\\\\\\"(?<year>.*?)\\\\)";
string aniPattern=#"(?si:<option value=""(?<year>.*?)\)";
For # escaped strings, you double the quotation mark to escape it, since backslash is not used.
Use two double quotes next to each other, like so: ""