Solved

String Concat

Posted on 2013-06-15
5
423 Views
Last Modified: 2013-06-15
Hi Experts!

This is supposed to be easy, but can't see it get it to work:

var str1 = "http://experts-exchange.com";
var str2 = "<p><a href=\" + str1 + ">Click Here</a></p>";

Open in new window

str2 should read:

<p><a href=\"http://experts-exchange.com">Click Here</a></p>

The error is that it expects ; and adding another ; will not work.

Any ideas?

TIA!
0
Comment
Question by:allanau20
5 Comments
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 39249783
you need to double up the quotes
 var str1 = "http://experts-exchange.com";
            var str2 = "<p><a href=\'" + str1 + "\">Click Here</a></p>";
0
 
LVL 12

Expert Comment

by:jitendra patil
ID: 39249808
you can do it this way too

var str1 = "http://experts-exchange.com";
            var str2 = "<p><a href=" + str1 + ">Click Here</a></p>";

hope this helps
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39249828
@patil786  That won't have the leading and ending quotation marks that he requires
My output

<p><a href='http://experts-exchange.com">Click Here</a></p>

Your output
<p><a href=http://experts-exchange.com>Click Here</a></p>

Desired Output (he has a typo)
<p><a href="http://experts-exchange.com">Click Here</a></p>

This has the quotes around http://experts-exchange.com
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39249949
The problem is that you have a backslash before the quotation here:

"<p><a href=\"

This tells the C# compiler that you intended to escape the quote, not insert a backslash. To insert a literal backslash, you double it up:

var str2 = "<p><a href=\\" + str1 + ">Click Here</a></p>";

Open in new window


ve3ofa has given you the answer as to inserting the quotation marks, but I would not describe it as "doubling up the quotes." In this example, yes, the quotes are apparently doubled. However, what you really need to be doing is escaping the quotes (like I mentioned with the backslash above). You do this with a backslash as well (exemplified in ve3ofa's first response). It is purely coincidental as to whether or not the quotes end up "doubled up."
0
 
LVL 5

Author Comment

by:allanau20
ID: 39250403
thank you all!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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 …

777 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