Solved

C#.NET - Escape Quotes

Posted on 2009-07-16
18
970 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for the delay, had to run to a meeting.  I'm reviewing these responses now.
0
 

Author Comment

by:scross1276
Comment Utility
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
Comment Utility
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
Comment Utility

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:scross1276
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
GuitarRich, this time I copied and pasted your string into my code and got 10 errors...
image.jpg
0
 

Author Comment

by:scross1276
Comment Utility
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
Comment Utility
sorry - in the hyperlink change the final slash to a single not a double:
pendingRevealRequests.aspx\\">
to
pendingRevealRequests.aspx\">
0
 

Author Comment

by:scross1276
Comment Utility
You nailed it.  Thanks!!
0
 
LVL 12

Expert Comment

by:GuitarRich
Comment Utility
no probs
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

10 Experts available now in Live!

Get 1:1 Help Now