Solved

String Concat

Posted on 2013-06-15
5
421 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 78

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 78

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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

12 Experts available now in Live!

Get 1:1 Help Now