Unquote string in C#
On your string use Trim with the " as char:
.Trim('"')
Strip double quotes from a string in .NET
I think your first line would actually work but I think you need four quotation marks for a string containing a single one (in VB at least):
s = s.Replace("""", "")
for C# you'd have to escape the quotation mark using a backslash:
s = s.Replace("\"", "");
How To Escape Quotation Marks in Multiple Line Of String? C#
To escape double quotes in a verbatim string (ie, a string declared with the @
prefix), simply double the quotes up (""
). This is in contrast to how you would normally escape a double quote in a string, \"
.
var stringHolder = @" book book ""book""
ten ten ""book"" book pen
pen ""hook book"" dook
beer poor ""111"" cat map";
Console.WriteLine(stringHolder);
/*Output:
book book "book"
ten ten "book" book pen
pen "hook book" dook
beer poor "111" cat map
*/
When the indentation matters, you may have to fight the normal tabbing in your editor a bit, which can lead to some odd looking declarations.
namespace MyNamespace {
public class Foo {
public string GetString() => @"Hello
World"; // Returns a string that looks like
// Hello
// World
public string GetString2() => @"Hello
World"; // Returns a string that looks like
// Hello
// World
}
}
Escape double quotes in a string
No.
Either use verbatim string literals as you have, or escape the "
using backslash.
string test = "He said to me, \"Hello World\" . How are you?";
The string has not changed in either case - there is a single escaped "
in it. This is just a way to tell C# that the character is part of the string and not a string terminator.
C# how to remove escaped quotes from string response
var token = shippingCalcualtion.token.Trim('"');
Trim(...)
will remove all given trailing and leading characters from your string. In this case '\"'
. Restsharp will deserialize what's given, so if it should deserialize "test"
(the value provided through the network stack) it will be deserialized to \"test\"
.
if you click the magnifying glass on your 2nd example you will get only Firefox
without "..."
. In this case the "
is not part of the string itself, but in shippingCalcualtion.token
the string contains one leading and trailing "
.
How to escape "\"String\"" in C#
Use String.Trim()
to remove the additional quotes around your string. Note, that this will only work for quotes on the very beginning or end of the string.
NewLocationId = NewLocationId.Trim('\"');
If you need the to also remove quotes inbetween the rest of the string, use String.Replace("\"", "")
:
Alternatively, if it is always the same "structure" always strip first and last character, this might execute faster:
NewLocationId = NewLocationId.Substring(1, NewLocationId.Length - 2);
Any way to use string (without escaping manually) that contains double quotes
No. In C# syntax, the only way to define string literals is the use of the double quote " with optional modifiers @ and/or $ in front. The single quote is the character literal delimiter, and cannot be used in the way PHP would allow - in any version, including the current 8.0.
Note that the PHP approach suffers from the need to escape ' as well, which is, especially in the English language, frequently used as the apostrophe.
To back that up, the EBNF of the string literal in current C# is still this:
regular_string_literal '"' { regular_string_literal_character } '"'
The only change in the compiler in version 8.0 was that now, the order of the prefix modifiers $ (interpolated) and @ (verbatim) can be either @$ or $@; it used to matter annoyingly in earlier versions.
Alternatives:
Save it to a file and use File.ReadAllText
for the assignment, or embed it as a managed ressource, then the compiler will provide a variable in the namespace of your choice with the verbatim text as its runtime value.
Or use single quotes (or any other special character of your choice), and go
var t = @"Text with 'many quotes' inside".Replace("'", @"""");
where the Replace
part could be modeled as an extension to the String
class for brevity.
How to avoid double quote escape in C# strings?
No, this is not possible in C#. You could write your SQL in a separate file and then read it from there.
how to convert char @"\" to Escape String \ by C#
You can use the System.Text.RegularExpressions.Regex.Unescape
method:
var input = @"\u5317\u4eac\u6c83\u534e\u521b\u65b0\u79d1\u6280\u6709\u9650\u516c\u53f8";
string escapedText = System.Text.RegularExpressions.Regex.Unescape(input);
Escape double quotes in a C# string
Just use @
for verbatim literal strings.
text.Replace(@"this", @"that");
Example:
text.Replace(@"\", @"\\").Replace(@"""", @"\""");
Related Topics
How to Secure the Asp.Net_Sessionid Cookie
Linq: Select an Object and Change Some Properties Without Creating a New Object
How to Convert Json Date to .Net Datetime During Deserialization
Passing a List of Objects into an MVC Controller Method Using Jquery Ajax
How to Check for a Valid Base64 Encoded String
How to Get User Browser Name ( User-Agent ) in ASP.NET Core
How to Wait Until Task Is Finished in C#
How to Fix Json.Net (Newtonsoft.Json) Runtime File Load Exception
Pass Variable in Json String in C#
Trying to Add Spaces Between Characters in a String in C#
Access Variables/Functions from Another Component
Copy Files With Authentication in C#
Read and Parse a Json File in C#
How to Check If a Socket Is Connected/Disconnected in C#
Asp.Net C# Button Onclick Even Not Firing
How to Calculate the Average of Each Row in Multidimensional Array
Client Certificate Not Getting Added to the Request (Certificate Verify)