Solved

String Concat

Posted on 2013-06-15
5
426 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 80

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 80

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

Industry Leaders: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

679 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