blade44
blade44

Reputation: 457

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 all of the original structure. How should I fix this? It's a little tricky as there are quotation marks within the string.

Upvotes: 34

Views: 114407

Answers (7)

Bogdan
Bogdan

Reputation: 1

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" }""";

Upvotes: 0

TheProgrammer
TheProgrammer

Reputation: 58

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

Upvotes: 0

Levente Boda
Levente Boda

Reputation: 53

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";

Upvotes: 2

rsenna
rsenna

Reputation: 11964

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";

Upvotes: 41

Abe Miessler
Abe Miessler

Reputation: 85036

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

Upvotes: 20

AbdouMoumen
AbdouMoumen

Reputation: 3854

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

Upvotes: 5

BrandonZeider
BrandonZeider

Reputation: 8152

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

Upvotes: 4

Related Questions