简体   繁体   中英

How do I store a string with multiple quotations mark in the data?

I have an input which I need to store in string so that I can manipulate, but I am unable to store as string because the input has multiple quotations mark.

For example

string s = "dfasdf" : "FASDFSD" ["FSADFSA"];

There are two ways to do it:

  • Use a regular string and escape the double quotes, or
  • Use a verbatim @"" string, and double the double quotes.


 "\"This string is quoted\""

or

 @"""This string is quoted"""

您可以通过写\\"而不是仅仅"来转义应被视为数据一部分的引号:

string s = "\"dfasdf\" : \"FASDFSD\" [\"FSADFSA\"]";

One solution is to double each quotation mark literal you want in the string, as such:

string s = """dfasdf"":""FASDFSD""[""FSADFSA""]";

Another is to escape them:

string s ="\"dfasdf\":\"FASDFSD\"[\"FSADFSA\"]";

使用转义是好的:

    string s = "\"dfasdf\" : \"FASDFSD\" [\"FSADFSA\"]";

您可以使用\\"来插入双引号

You can include double quotes character in verbatim string(refer note below) by writing it twice

so if you need to store

hello "Hi!"

You need to store it like this

string s=@"hello ""Hi!"" ";

OR

escape it using \\

string s="hello \"Hi!\" ";

NOTE

A verbatim string don't interpret control characters like \\n,\\r..So when \\n,\\r are used in verbatim strings,it is never interpreted.

So string s="hello \\n hi!";

would be printed as

hello \n hi!

not

hello
hi!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM