Tuesday, 16 August 2016

c# - Escape double quotes in string



Double quotes can be escaped like this:




string test = @"He said to me, ""Hello World"". How are you?";


But this involves adding character " to the string. Is there a C# function or other method to escape double quotes so that no changing in string is required?


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...