I am trying to create a user macro with a body that takes in a string (ex. Hello - World). How do I store each word separated by the "-" in a variable? Below is what I have tried:
#set ($var1 = $body.split("-"))
#set ($var2 = $var1[0]) // also tried $var1.get(0). Should be "Hello"
#set ($var3 = $var1[1]) // also tried $var1.get(1). Should be "World"
I cannot get this to work for some reason. Any help would be appreciated!
Thanks,
Brian
I think your issue is that the split method use a Regex string
split(String regex, int limit)
And hyphen is used to indicate range, so try escaping it in your expression.
#set ($var1 = $body.split("\-"))
Still doesn't work. It breaks the macro if I try to access the values using either $var1.get(0) or $var1[0].
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, the issue is with the indexing. Split returns a string list, and I am not smart enough to figure out how to address via index. But the following code could give you a work around.
## @noparams
#set ($var1 = $body.split("-"))
#foreach($string in $var1)
<p>$string</p>
#end
You could then create an array, and then assign them in order.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for solution. I made another variant solution, based on yours, with "indexing" and it works :)
## @noparams
#set ($username = $action.getAuthenticatedUser().name)
#set ($fullName = $action.userAccessor.getUserByName($username).fullName)
#set ($var = $fullName.split(" "))
#set ($i = 0)
#foreach($string in $var)
#set($i = $i + 1)
#if ($i == 2)
$string
#end
#end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.