如何在C#中双引号字符串

Kab*_*abi 0 c#

我想生成一个像这样的字符串: "blabla"

我试着用单引号包装它:"'blabla'"但是这会产生'blabla'.如何用双引号包装字符串?

Tim*_*ora 13

我假设(因为问题不清楚而大的假设)你想在你的字符串中加双引号.为此,您可以使用逐字字符串文字语法.

string s = @"""my string""";
Run Code Online (Sandbox Code Playgroud)

您也可以手动转义双引号:

string s = "\"my string\"";
Run Code Online (Sandbox Code Playgroud)

其他示例(来自MSDN)

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";
Run Code Online (Sandbox Code Playgroud)

  • `@""我的字符串""`=大问题. (3认同)

Cuc*_*ser 5

string s = "\"blabla\"";
Run Code Online (Sandbox Code Playgroud)

这将使 " 保持在字符串内

  • 它唯一能做的就是阻止代码编译。:) 编辑:原始代码是`string s = "\"blabla"\";` 我有点像个白痴,但没有被纠正。:D (3认同)