Solved

String Concat

Posted on 2013-06-15
5
427 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
[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
5 Comments
 
LVL 81

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 81

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust the codes 3 63
CSS question 16 62
C# DateTime Help 6 46
Which is best Image Resizing Web service 11 39
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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