12

I want to print two double quotes in C# as the output. How to do this?

I mean the output should be: "" Hello World ""

0

11 Answers 11

22
Console.WriteLine("\"\" Hello world \"\"");

or

Console.WriteLine(@""""" Hello world """"");
3
  • 2
    Ah, you just beat me. Such a simple question, and so many incorrect answers!
    – Mr Lister
    Mar 6, 2012 at 11:33
  • What does 4 double quote ("""") mean?
    – monish001
    Nov 21, 2012 at 8:32
  • 2
    @Monish "" is a single double quote within a @"..." verbatim string literal. Nov 23, 2012 at 14:43
7

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)

2

One way is to escape the quotes:

var greeting = "\"Hello World\"";
2

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"
0
2

you can output with the @, which will automatically escape special characters.

string output = "\"\" Hello World \"\"";

string output = @""""" Hello World """"";
2

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.

1

Escape them:

Console.WriteLine("\"Hello world\"");
0
1
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.

1

Use a backslash before the double quotes: \"

0

Using a @-char before the 'normal' double quotes will result in printing every special char between those dubble quotes

string foo = @"foo "bar"";
2
  • 1
    In a verbatim string (@"") you need to use 2 quote characters, i.e. "". What you have is wrong. The OP would want something like @""""" Hello World """"". Mar 6, 2012 at 11:34
  • Indeed. the double quote is in fact the only special char in an "@string" (I forget the real name for them). This is becasue clearly an @ is the terminator of the string so it needs to be escaped if being used to not terminate a string.
    – Chris
    Mar 6, 2012 at 11:38
-2

StringBuilder sb = new StringBuilder(); sb.Append("\"Hello World \""); string s = sb.ToString();

1
  • Why would you use a StringBuilder for escaping a string? Mar 6, 2014 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.