A string strChkQoutes is
IF(H15:H119=\"y\",IF(G15:G119=\"y\",1,0)
The following value is true(c#).
strChkQoutes.Contains(#"""")
I don't understand it's meaning. If I want to convert it to java, the string strChkQoutes is
IF(H15:H119="y",IF(G15:G119="y",1,0)
the following value is false(java).
strChkQoutes.contains("\"\"")
what is the difference of the contains function in .net and in java?
The difference here doesn't lie in the methods, but the strings you're passing to the methods.
In C# verbatim string literals, #"""" really means one double quote character. The first inner " escapes the second inner ", since you can't use backslashes for escaping. Reference.
If you didn't use a verbatim string literal, the C# call would look like this:
strChkQuotes.Contains("\"")
Which is different from your Java string, which contains two escaped double quotes in a row and so causes contains() to return false.
# is a C# String literal that java does not have. In Java you'd have to escape your string: .contains("\""). See here for how #-literals are resolved.
Related
So in microsoft visual studio I have a string that is compiled into a regex. My string is "#(\d+(.\d+)?)=(\d+(.\d+)?)". I cannot compile my program because I get an error saying that \d is a unrecognized escape character. How do I tell it to shut up and let me regex like a pro?
Begin your string with #, that causes the compiler to leave (almost) all characters alone, unescaped (the exception is ", which can be escaped as ""):
#"#(\d+(.\d+)?)=(\d+(.\d+)?"
The problem is that c# does not like the \d inside the string. Use a verbatim string instead
string pattern = #"#(\d+(.\d+)?)=(\d+(.\d+)?)";
The "#" denotes it. C# will not look for escape sequences in the string. If you have to escape a " use two "".
Of cause you can use normal strings. but then you will have to escape the backslashes
string pattern = "#(\\d+(.\\d+)?)=(\\d+(.\\d+)?)";
If you're using a normal string, you need to escape your backslashes, like so:
"#(\\d+(.\\d+)?)=(\\d+(.\\d+)?)"
Basically, you're putting a literal string into C#; the C# compiler sees the string first, and tries to interpret \d as an escape sequence (which doesn't exist, hence error). Therefore, you use \\d to get the C# compiler to see the string as \d, which then gets passed to the regex engine (which does recognize \d as something meaningful). (yes, if you want to match a literal backslash in your regex pattern, you need to use \\\\)
But in C#, you have the alternative of just prepending the string with # to get the compiler to leave the string alone (though " still needs escaping), so that would be like this:
#"#(\d+(.\d+)?)=(\d+(.\d+)?)"
You could also use a verbatim string literal (I prefer to use these because of readability).
Use #"(#\d+(.\d+)?)=(\d+(.\d+)?)"
The #" sign indicates that the string shouldn't interpret escaped characters (A character prefixed by a \) until the closing " is reached.
Note: You can match a single " in your search pattern by double quoting instead "". For instance you can match "Hello" by using the pattern #"""\w+"""
I am using .NET (C#) code to write to a database that interfaces with a Perl application. When a single quote appears in a string, I need to "escape" it. IOW, the name O'Bannon should convert to O\'Bannon for the database UPDATE. However, all efforts at string manipulation (e.g. .Replace) generate an escape character for the backslash and I end up with O\\'Bannon.
I know it is actually generating the second backslash, because I can read the resulting database field's value (i.e. it is not just the IDE debug value for the string).
How can I get just the single backslash in the output string?
R
Well I did
"O'Bannon".Replace("'","\\'")
and result is
"O\'Bannon"
Is this what you want?
You can use "\\", which is the escape char followed by a backslash.
See the list of Escape Sequences here: http://msdn.microsoft.com/en-us/library/h21280bw.aspx
even better assign a var to the replace so that you can check it as well if needed
var RepName = "O'Bannon";
var Repstr = RepName.Replace("'","\\'");
You can also use a verbatim string
s = s.Replace("'", #"\'");
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 have been asked to document some code. Some javascript functions pass parameters like
onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>);'
What is the purpose of "\" in the code?
Are they some special kind of escape characters? Would the code fail if we remove them?
They are 'escaping' the quotation marks, so they can be included in the string. Otherwise they would be confused with the start/end quotation marks of the string.
http://en.wikipedia.org/wiki/Escape_character
This doesn't look like (pure) JavaScript, but instead like some other language that produces JavaScript code (probably ASP.NET):
Assuming that <%# %> is that languages code to insert the result of the contained statement into the text, this means that the result of
String.Format("\"#customer{0}\"",Container.DataItemIndex)
will be written between the closing and the opening parenthesis of the function call.
This means that the escape character \ isn't use in JavaScript here, but in the host language (probably C# or VB.NET). The meaning is probably the same as in JavaScript, 'though: it escapes the double-quote to allow it to be represented inside a string literal.
What is the purpose of "\" in the code ?
Seems like escape sequences to me. Assuming that this is C# code, the string in your code:
"\"#customer{0}\""
is interpreted by the compiler as:
"#customer{0}"
(this INCLUDES the double quotes)
When this is response.written, the output will become:
onclick='showhide("#customer_1234");'
Yes, it is an escape character.
In this case it is escaping the " character.
It is an escape sequence used in the above code.
When written in string \" is equivalent to ". i.e.,
<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>
would render something like this if DataItemIndex is 9
"#customer9"
I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it like I would with the literal command?
Instead of this:
string filePath = #"C:\somepath\file.txt";
I want to do this:
string filePath = #helper.getFilePath(); //getFilePath returns a string
This isn't how I am actually using it; it is just to make what I mean a little clearer. Is there some sort of .ToLiteral() or something?
I don't think you have to worry about it if you already have the value. The # operator is for when you're specifying the string (like in your first code snippet).
What are you attempting to do with the path string that isn't working?
I'm not sure if I understand. In your example: if helper.getFilePath() returns "c:\somepath\file.txt", there will be no problem, since the # is only needed if you are explicitely specifying a string with "".
When Functions talk to each other, you will always get the literal path. If the XML contains c:\somepath\file.txt and your function returns c:\somepath\file.txt, then string filePath will also contain c:\somepath\file.txt as a valid path.
The #"" just makes it easier to write string literals.
string (C# Reference, MSDN)
Verbatim string literals start with # and are also enclosed in double quotation marks. For example:
#"good morning" // a string literal
The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
#"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
One place where I've used it is in a regex pattern:
string pattern = #"\b[DdFf][0-9]+\b";
If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.
In C# the # symbol combined with doubles quotes allows you to write escaped strings. E.g.
print(#"c:\mydir\dont\have\to\escape\backslashes\etc");
If you dont use it then you need to use the escape character in your strings.
http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx
You dont need to specify it anywhere else in code. In fact doing so should cause a compiler error.
You've got it backwards. The #-operator is for turning literals into strings, while keeping all funky characters. Your path is already a string - you don't need to do anything at all to it. Just lose the #.
string filePath = helper.getFilePath();
The string returned from your helper class is not a literal string so you don't need to use the '#' character to remove the behaviour of the backslashes.