Solved

C#.NET - Escape Quotes

Posted on 2009-07-16
18
976 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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