majji
majji

Reputation: 169

how to put the string with in href (<a href=>Demo</a>) by using escape sequences?

I have the following code snippet and i am trying add string demoString to href.

String demoString = "/helio/demo";
String demoStrUrl = "<a href=demoString ><h3 class="+"demoDetails" +">Details</h3></a>"

Upvotes: 0

Views: 303

Answers (3)

Sage
Sage

Reputation: 15418

First escape the quote of demoString:

String demoString = "\"/helio/demo\"";

Then add it to demoStrUrl using + operator:

String demoStrUrl = "<a href="+demoString+" ><h3 class.....

To facilitate with escaping HTML you can look into the apache commons StringEscapeUtils class.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Simply:

String demoString = "/helio/demo";
String demoStrUrl = "<a href=\"" + demoString + "\"><h3 class=\"demoDetails\">Details</h3></a>";

Note that if you want to add a " sign within the String, you have to escape it with \

Upvotes: 1

noone
noone

Reputation: 19776

You have to append String variables as an object using + only. Otherwise it would again be handled like a fixed String and not your defined variable.

String demoString = "/helio/demo";
String demoStrUrl = "<a href=\""+demoString+"\" ><h3 class=\"demoDetails\">Details</h3></a>";

Upvotes: 0

Related Questions