Solved

C#.NET - Escape Quotes

Posted on 2009-07-16
18
984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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