Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am new in C# and want using String Variable in my code. For define String I must use "".
For example: string str="reza".
My question:
If want use string that this string is include "" how must do that?

For example:
string str=
C#
"<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<p>Hi,</p>
<p>This is my first string.</p>
<p>Regards,</p>

</body>
";


Regards,
Posted
Updated 28-Aug-13 6:38am
v2

Change every " inside your string into a \" (a backslash followed by a quotation mark). This is the escape sequence for a double quotation mark.
More about escape sequences: http://msdn.microsoft.com/en-us/library/h21280bw.aspx[^]

Example:
C#
string str = "In this string, an \"escape sequence\" is used";
 
Share this answer
 
v2
Comments
Mehdi Gholam 28-Aug-13 12:51pm    
5'ed
Thomas Daniels 28-Aug-13 12:51pm    
Thank you!
Adarsh chauhan 29-Aug-13 2:37am    
+5
Thomas Daniels 29-Aug-13 2:37am    
Thank you!
You have to use double double quotes "" or delimit with a slash like \" in c# strings.

A better option is to embed your text as a resource and load it into your string.
 
Share this answer
 
Comments
Rezame 28-Aug-13 12:47pm    
Can you explain more?
How must do that?
Mehdi Gholam 28-Aug-13 12:52pm    
see this : http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file
Thomas Daniels 28-Aug-13 12:52pm    
My vote of 4: your answer is almost perfect, but I suggest to add that you can only use the double double quotes if you added a verbatim string literal (the @ sign).
Mehdi Gholam 28-Aug-13 12:53pm    
Yep, thanks!
Two ways:

string str = "In this string, an \"escape sequence\" is used"; (Previously mentioned)

alternative way:

string str = @"In this string, an ""escape sequence"" is used";
 
Share this answer
 
Comments
[no name] 28-Aug-13 16:54pm    
Your solution is just to do either solution 1 or solution 2? Exactly how does your solution differ in any way?
MTProgrammer 28-Aug-13 17:24pm    
It wasn't clear about the verbatim @ so I just spelled it out is all. You are correct, I should have made a comment, not a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900