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
Solved

String Concat

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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