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\"")
Related
I'm trying to extract information out of rc-files. In these files, "-chars in strings are escaped by doubling them ("") analog to c# verbatim strings. is ther a way to extract the string?
For example, if I have the following string "this is a ""test""" I would like to obtain this is a ""test"". It also must be non-greedy (very important).
I've tried to use the following regular expression;
"(?<text>[^""]*(""(.|""|[^"])*)*)"
However the performance was awful.
I'v based it on the explanation here: http://ad.hominem.org/log/2005/05/quoted_strings.php
Has anybody any idea to cope with this using a regular expression?
You've got some nested repetition quantifiers there. That can be catastrophic for the performance.
Try something like this:
(?<=")(?:[^"]|"")*(?=")
That can now only consume either two quotes at once... or non-quote characters. The lookbehind and lookahead assert, that the actual match is preceded and followed by a quote.
This also gets you around having to capture anything. Your desired result will simply be the full string you want (without the outer quotes).
I do not assert that the outer quotes are not doubled. Because if they were, there would be no way to distinguish them from an empty string anyway.
This turns out to be a lot simpler than you'd expect. A string literal with escaped quotes looks exactly like a bunch of simple string literals run together:
"Some ""escaped"" quotes"
"Some " + "escaped" + " quotes"
So this is all you need to match it:
(?:"[^"]*")+
You'll have to strip off the leading and trailing quotes in a separate step, but that's not a big deal. You would need a separate step anyway, to unescape the escaped quotes (\" or "").
Don't if this is better or worse than m.buettner's (guessing not - he seems to know his stuff) but I thought I'd throw it out there for critique.
"(([^"]+(""[^"]+"")*)*)"
Try this (?<=^")(.*?"{2}.*?"{2})(?="$)
it will be maybe more faster, than two previous
and without any bugs.
Match a " beginning the string
Multiple times match a non-" or two "
Match a " ending the string
"([^"]|(""))*?"
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("'", #"\'");
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.
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: ""