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"
Related
I have the following regular expression for one of my name fields in C# web app:
^[A-Za-zÀ-ſ0-9.,#&-/'_!#;]?[a-zA-ZÀ-ſ0-9 '#&-/.,_:!#;]*[A-Za-zÀ-ſ0-9.,#-/_!#;]$
How can I properly modify it to add apostrophe/single quote character (') as an allowed character to it?
' is used for declaring a char, so put a backslash in front of the ' to escape it, like this \'.
It turned out that the RegEx has been fine, and it was the way the data has been input into database that caused the problem. Insert statements should have the apostrophes escaped. Even though the apostrophes were getting displayed correctly, they had been failing the RegEx check due to lack of escaping apostrophes. Thanks for your advice and sorry in case of any disapointment!
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+"""
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\"")
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.
My program outputs strings like "Wzyryrff}av{v5~fvzu: Bb``igbuz~+\177Ql\027}C5]{H5LqL{" and the problem is the escape codes (\\\ instead of \, \177 instead of the character, etc.)
I need a way to unescape the string of all escape codes (mainly just the \\\ and octal \027 types). Is there something that already does this?
Thanks
Reference: http://www.tailrecursive.org/postscript/escapes.html
The strings are an encrypted value and I need to decrypt them, but I'm getting the wrong values since the strings are escaped
It sounds more like it's encoded rather than simply escaped (if \177 is really a character). So, try decoding it.
There is nothing built in to do exactly this kind of escaping.
You will need to parse and replace these sequences yourself.
The \xxx octal escapes can be found with a RegEx (\\\d{3}), iterating over the matches will allow you to parse out the octal part and get the replacement character for it (then a simple replace will do).
The others appear to be simple to replace with string.Replace.
If the string is encrypted then you probably need to treat it as binary and not text. You need to know how it is encoded and decode it accordingly. The fact that you can view it as text is incidental.
If you want to replace specific contents you can just use the .Replace() method.
i.e. myInput.Replace("\\", #"\")
I am not sure why the "\" is a problem for you. If it its actually an escape code then it just should be fine since the \ represents the \ in a string.
What is the reason you need to "remove" the escape codes?