Solved

C#.NET - Escape Quotes

Posted on 2009-07-16
18
971 Views
Last Modified: 2013-11-05
Can someone tell me how to escape the double quotes and double forward slashes in my example below?
Please click here (" + <a href="http://www.google.com>Google</a> + ") soon.";

Open in new window

0
Comment
Question by:scross1276
  • 7
  • 7
  • 2
  • +2
18 Comments
 
LVL 5

Expert Comment

by:Krummelz
ID: 24868960
Easy:
Change the double quotes that you want to escape, into single quotes. They work the same. And for the forward slashes, you can escape them by adding a backslash in front or each one.

If that doesnt work for you, you can always try making the string a literal string by adding an @ symbol in front of your string value eg. string myString = @"this is my string value"; Then you do not need to escape the special characters.
0
 

Expert Comment

by:acortis
ID: 24869269
In order to add double quotes within a string, what I usually do is to include two double quotes after each other to indicate that this is only a double quote contained within the string and not the end of string.
Since the / is used as an escape character, you need to use a double / (//) in order to indicate that you want a backslash and not use it as an escape character.
I am assuming that you are creating a string to be placed in a label for the example, but the concept still applies.
Your result should be as shown in the code snippet.
"Please click here (<a href=""http:////www.google.com"">Google<//a>) soon."

 

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24869292
backslash is the escape chr for c# so assign your string like this. The forward slashes are not marked as special in C# so they don't need escaping. They are fine as is.
 

string s = "Please click here (<a href=\"http://www.google.com\">Google</a>) soon.";

 

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24869308
Just FYI - double quotes are the VB way of doing it and \" is the C# - also BACKslash is the escape chr - not FORWARDslash like you have in your string.
0
 
LVL 15

Expert Comment

by:jinal
ID: 24869335
1.  Please click here (" + <a href='http://www.google.com'>Google</a> + ") soon.";

2. Please click here (" + <a href=\""http://www.google.com\"">Google</a> + ") soon.";
0
 

Author Comment

by:scross1276
ID: 24869757
Sorry for the delay, had to run to a meeting.  I'm reviewing these responses now.
0
 

Author Comment

by:scross1276
ID: 24869832
Actually none of the solutions worked.  I've included my entire string below.  The code is going into a generated email in my code behind page.  I want to include a hyperlink in the email, but can't figure out how to escape the double quotes and double forward slashes needed for the anchor tag.

I've also tried it using Response.Write("<a href...></a>") but still have problems.
message.Body = "A new report request has been received for " + txtName.Text + " (" + txtID.Text + ").  Please click here (" + <a href="http://server/eforms/reveal/pendingRevealRequests.aspx">Pending eForms</a> + ") soon.";

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24869919
You should set your message body like this. Also make sure that the message type is set to html otherwise it wont work.

 

message.Body = "<p>A new report request has been received for " + txtName.Text + " (" + txtID.Text + ").  Please click here (<a href=\"http://server/eforms/reveal/pendingRevealRequests.aspx\\">Pending eForms</a> ) soon.</p>";
 
 

    

Open in new window

0
 
LVL 15

Expert Comment

by:jinal
ID: 24869978

Message.Body = "A new report request has been received for " + txtName.Text +  "(" + txtID.Text + ").  Please click here (<a href='http://server/eforms/reveal/pendingRevealRequests.aspx'>Pending eForms</a> + soon.";

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:scross1276
ID: 24869983
How do I set the message type?

I have my string set to match your exactly and have eight build errors on it.
image.jpg
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24870020
your string is not exactly as I typed. You still have the (" + < bit before the hyperlink.  The hyperlink is part of the string, you shouldn't close the string and then append the hyperlink. Try copying my code and pasting it into your code and the errors will go.


My code :

message.Body = "<p>A new report request has been received for " + txtName.Text + " (" + txtID.Text + ").  Please click here (<a href=\"http://server/eforms/reveal/pendingRevealRequests.aspx\\">Pending eForms</a> ) soon.</p>";

Your code:

message.Body = "<p>A new report request has been received for " + txtName.Text + " (" + txtID.Text + ").  Please click here (" + <a href=\"http://server/eforms/reveal/pendingRevealRequests.aspx\\">Pending eForms</a> + ") soon.</p>";

Open in new window

0
 

Author Comment

by:scross1276
ID: 24870022
jinal, your solution does work, however it does not present it as a hyperlink because you've removed the double quotes  surrounding the anchor tag, so the compiler just reads the link as plain text.  I need it to read it as a hyperlink.
0
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 500 total points
ID: 24870040
To make sure the body is html set the IsBodyHtml flag on the message object:
 

message.IsBodyHtml = true;

Open in new window

0
 

Author Comment

by:scross1276
ID: 24870073
GuitarRich, this time I copied and pasted your string into my code and got 10 errors...
image.jpg
0
 

Author Comment

by:scross1276
ID: 24870095
Thanks for the IsBodyHtml tip.  I've added it.  I still have the errors shown in my latest screenshot though.
0
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 500 total points
ID: 24870191
sorry - in the hyperlink change the final slash to a single not a double:
pendingRevealRequests.aspx\\">
to
pendingRevealRequests.aspx\">
0
 

Author Comment

by:scross1276
ID: 24870216
You nailed it.  Thanks!!
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24870319
no probs
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 50
Hovering effect 9 47
Replace &lt; with < 14 56
Gridview alignment 1 10
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now