Solved

C#.NET - Escape Quotes

Posted on 2009-07-16
18
974 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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