I have a string like this
'Name.ToLower().Contains ("") And Created < ("05/12/2022 01:41:16") And Disabled == false'
how to deal with this in c#
I've tried pre-pending with @""" etc.
Thanks for looking.
CodePudding user response:
var a = @"'Name.ToLower().Contains ("""") And Created < (""05/12/2022"") And ExpandOnStart == false'";
like this?
CodePudding user response:
Obviously you have to escape special characters somehow. In older C# use verbatim strings: https://dotnetfiddle.net/8OtiKP
@"'Name.ToLower().Contains ("""") And Created < (""05/12/2022"") And ExpandOnStart == false'"
As you can see, "
still needs to be escaped with ""
, but all the other characters, including \
can be left as-is
In C#11 there's a better solution: raw string literal where there's no need to escape "
at all
"""'Name.ToLower().Contains ("") And Created < ("05/12/2022 01:41:16") And Disabled == false'"""
CodePudding user response:
This is a bit different approach, but you mentioned that the string is created by appending other strings, so possibly it'll help.
var createdDate = "05/12/2022";
var name = "some name";
var expandOnStart = "false";
var theString = $"Name.ToLower().Contains(\"{name}\") And Created < (\"{createdDate}\") And ExpandOnStart == \"{expandOnStart}\"";
// theString = Name.ToLower().Contains("some name") And Created < ("05/12/2022") And ExpandOnStart == "false"