Reputation: 992
I am trying to work with Velocity, which is like programming with your feet. I'm fluent in JavaScript and PHP but not Java. So I need a little help figuring out the correct syntax for this. I've done some research but since I'm not familiar with Java and Velocity is cringy, I can't figure out the right syntax.
I have a string, $string that is a url. I need to split it at the anchor # and reorganize it so that it will be in proper form for parameters and an anchor tag.
WHAT I HAVE:
URL ($string): http://url.com#anchorlink
PARAMETERS (plain text): ?utm_campaign=abc&utm_medium=def
WHAT I'M TRYING TO DO:
Split the string at # and reconsitute the url to:
http://url.com?utm_campaign=abc&utm_medium=def#anchorlink
HERE'S WHAT I HAVE IN CODE that I need help putting into proper syntax (it's wrong here):
<div>
#set ($myArray = $string.split("#"));
<a href = "${myArray[0]}?utm_campaign=abc&utm_medium=def${myArray[1]}">
Link Text
</a>
</div>
Thanks!
Upvotes: 1
Views: 256
Reputation: 4971
You need to include a #
yourself after def
in the URL as the value of myArray
will be {"http://url.com", "anchorlink"}
, without the #
in either part. The below template should work:
<div>#set ($myArray = $string.split("#"))
<a href = "${myArray[0]}?utm_campaign=abc&utm_medium=def#${myArray[1]}">
Link Text
</a>
</div>
The template returns the below result when I tested using string=http://url.com#anchorlink
with a tester that I wrote:
<div>
<a href = "http://url.com?utm_campaign=abc&utm_medium=def#anchorlink">
Link Text
</a>
</div>
You could get the same result also using below template, which uses String.substring
instead of split
:
<div>
<a href = "${string.substring(0, $string.indexOf('#'))}?utm_campaign=abc&utm_medium=def${string.substring($string.indexOf('#'), $string.length())}">
Link Text
</a>
</div>
Upvotes: 1