C# two double quotes

前端 未结 11 1490
灰色年华
灰色年华 2020-12-10 03:54

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

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

相关标签:
11条回答
  • 2020-12-10 04:33

    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)

    0 讨论(0)
  • 2020-12-10 04:33

    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 讨论(0)
  • 2020-12-10 04:39

    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.

    0 讨论(0)
  • 2020-12-10 04:43

    One way is to escape the quotes:

    var greeting = "\"Hello World\"";
    
    0 讨论(0)
  • 2020-12-10 04:45
    Console.WriteLine("\"\" Hello world \"\"");
    

    or

    Console.WriteLine(@""""" Hello world """"");
    
    0 讨论(0)
  • 2020-12-10 04:46
    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.

    0 讨论(0)
提交回复
热议问题