开发者

How to use a string with quotation marks inside it?

I have the following string, which I want to execute as a process:

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

However, given the presence of quotation marks, I can't insert this string in C# to make it compile, keeping开发者_如何学JAVA all of the original structure. How should I fix this? It's a little tricky as there are quotation marks within the string.


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

or

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


You can put @ in front of the string definition and put two ":

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

You can read more about escaping characters in strings in this article:

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


you have to escape the quotation marks using \. to have a string that says: Hello "World" you should write "Hello\"World\""


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


You can also always use Convert.ToChar(34), 34 being the ASCII of ".

for eg:

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

equals:

commit * -m "messBox.Text";


Starting with C# 11, it is possible to use three quotes (""") at the beginning and end of a string, instead of just one as usual:

string s = """{ "Id": 1, "Name": "Alex" }""";


Just put in a backslash \ before the quotation marks as needed:

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

The string now contains: This is where you put "your" string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜