Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
blade44 Avatar asked Apr 04 '11 15:04

blade44


People also ask

How do you use quotation marks in a string?

Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.

How do you escape quotation marks in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

Do strings have quotation marks?

Strings in JavaScript are contained within a pair of either single quotation marks '' or double quotation marks "". Both quotes represent Strings but be sure to choose one and STICK WITH IT. If you start with a single quote, you need to end with a single quote.

How do you quote inside a string in Python?

To quote a string in Python use single quotation marks inside of double quotation marks or vice versa. For instance: example1 = "He said 'See ya' and closed the door." example2 = 'They said "We will miss you" as he left.


5 Answers

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";
like image 184
rsenna Avatar answered Oct 04 '22 10:10

rsenna


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

like image 27
Abe Miessler Avatar answered Oct 04 '22 10:10

Abe Miessler


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

like image 23
AbdouMoumen Avatar answered Oct 04 '22 11:10

AbdouMoumen


string s = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";
like image 29
BrandonZeider Avatar answered Oct 04 '22 09:10

BrandonZeider


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";
like image 39
Levente Boda Avatar answered Oct 04 '22 10:10

Levente Boda