如何在里面使用带引号的字符串?

时间:2011-04-04 15:25:08

标签: c#

我有以下字符串,我想将其作为一个过程执行:

Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf

但是,如果存在引号,我无法在C#中插入此字符串以使其进行编译,从而保留所有原始结构。我该怎么解决这个问题?这有点棘手,因为字符串中有引号。

6 个答案:

答案 0 :(得分:34)

string whatever = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";

string whatever = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf";

答案 1 :(得分:12)

您可以将@放在字符串定义的前面并放置两个"

string myString = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf"

您可以在本文中阅读有关字符串中转义字符的更多信息:

http://www.yoda.arachsys.com/csharp/strings.html

答案 2 :(得分:4)

string s = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";

答案 3 :(得分:4)

您必须使用\ 转义引号。要有一个字符串:Hello "World"你应该写"Hello\"World\""

答案 4 :(得分:1)

您也可以使用Convert.ToChar(34),34是"的ASCII。

例如:

gitInfo.Arguments = @"commit * " + "-m" + Convert.ToChar(34) + messBox.Text + Convert.ToChar(34);

等于:

commit * -m "messBox.Text";

答案 5 :(得分:0)

只需根据需要在引号前加上反斜杠:

string yourString = "This is where you put \"your\" string";

字符串现在包含:This is where you put "your" string