C# two double quotes

前端 未结 11 1489
灰色年华
灰色年华 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: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.

      /// 
        /// Put a string between double quotes.
        /// 
        /// Value to be put between double quotes ex: foo
        /// double quoted string ex: "foo"
        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.

提交回复
热议问题