我有一个字符串“ I want to learn“ c #”。如何在 c # 之前和之后包含引号?
Escape them with backslashes.
"I want to learn \"C#\""
Use escape characters for example this code:
var message = "I want to learn \"c#\""; Console.WriteLine(message);
will output:
I want to learn "c#"
As well as escaping quotes with backslashes, also see SO question 2911073 which explains how you could alternatively use double-quoting in a @-prefixed string:
string msg = @"I want to learn ""c#""";
string str = @"""Hi, "" I am programmer";
OUTPUT - "Hi, " I am programmer
I use:
var value = "'Field1','Field2','Field3'".Replace("'", "\"");
as opposed to the equivalent
var value = "\"Field1\",\"Field2\",\"Field3\"";
Because the former has far less noise than the latter, making it easier to see typo's etc.
I use it a lot in unit tests.
The Code:
string myString = "Hello " + ((char)34) + " World." + ((char)34);
Output will be:
Hello "World."
You can also declare a constant and use it each time. neat and avoids confusion:
const string myStrQuote = "\"";