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: ""
Related
I am trying to format a text so I can provide a template some RFT text.
My string is declared with the stringformater as:
var fullTitleString = string.Format(
CultureInfo.CurrentCulture,
"{{\\Test",
title,
filterName);
But I keep obtaining a string as "{\Test". Using a single backslash results on errors at it does not understand the \T escaped character.
Doing #"{{\Test" also yields "{\Test". I have looked over the MSDN documentation and other questions where they tell to use another backslash as escaping character, but it doesn't seem to work.
There are two levevls of escaping here:
1. Escaping string literals
In c# strings, a backslash (\) is used as special character and needs to be escaped by another \. So if your resulting string should look like \\uncpath\folder your string literal should be var s = "\\\\uncpath\\folder".
2. Escape format strings
string.Format uses curly braces for place holders, so you need to escape them with extra braces.
So let's say you have
string title = "myTitle";
string filterName = "filter";
then
string.Format("{{\\Test {0}, {1}}}", title, filterName);
results in
{\Test myTitle, filter}
If you want two curly braces at the beginning, you need to put four in your format string:
string.Format("{{{{\\Test {0}, {1}}}", title, filterName);
results in
{{\Test myTitle, filter}
If you provide a clear example of what you are trying to achieve, I may tell you the correct format string.
Side note: In C# 6 the last example could also be $"{{{{\\Test {title}, {filterName}}}" (using string interpolation without explicitly calling string.Format)
NOTE: The Visual Studio debugger always shows the unescaped string literal. So if you declare a string like string s = "\\" you will see both backslashes in your debugger windows, but if you Console.WriteLine(s) only one backslash will be written to console.
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'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\"]"
in C#, can I use .Contains to check if a string contains a value within quotation marks?
e.g., if the string I'm evaluating contains
He said "something"
I want to do something like:
strEval.Contains("He said "something"")
Yes. You will need to escape the quotes, so they do not terminate the string:
strEval.Contains("He said \"something\"");
Have look at MSDN on escape sequences in C# strings.
This problem is unrelated to string.Contains. The real question is how to write a string literal containing " in C#. For this there are several possibilities:
Escape it with a \: "He said \"something\""
Use an verbatim string prefixed with # where you duplicate the ": #"He said ""something""". This is mainly useful if the original string contains many backslashes, such as in a regex.
Use the hex value of ", but that's not a good idea.
You have to just escape the quotation mark :
strEval.Contains("He said \"something\"")
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 ".