I want to print two double quotes in C# as the output. How to do this?
I mean the output should be: \"\" Hello World \"\"
If you want to put double quotes in a string you need to escape them with a \
eg:
string foo = "here is a \"quote\" character";
If you want to literally output "" Hello World ""
then you'd need:
string helloWorld = "\"\" Hello World \"\"";
output(helloWorld);
(where output is whatever method you are using for output)
When you want to use special character which are exist in you language add \ before that character then special character start behaving as a string. In your case use like this
\"Hello word\"
Out put
"Hello word"
If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.
This is really obvious code, but still I think it can be useful to grab and make you save time.
/// <summary>
/// Put a string between double quotes.
/// </summary>
/// <param name="value">Value to be put between double quotes ex: foo</param>
/// <returns>double quoted string ex: "foo"</returns>
public static string PutIntoQuotes(this string value)
{
return "\"" + value + "\"";
}
Then you may call foo.PutIntoQuotes() or "foo".PutIntoQuotes(), on every string you like.
Hope this help.
One way is to escape the quotes:
var greeting = "\"Hello World\"";
Console.WriteLine("\"\" Hello world \"\"");
or
Console.WriteLine(@""""" Hello world """"");
Console.WriteLine("\"\"Hello world\"\"");
The backslash ('\') character precedes any 'special' character that would otherwise be interpreted as your code instead of as part of the string to be output. It's a way of telling the compiler to treat it as a character part of a string as opposed to a character that would otherwise have some sort of purpose in the C# language.