Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to split the body in a user macro?

Brian Sung October 24, 2018

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

1 answer

1 accepted

0 votes
Answer accepted
Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 26, 2018

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("\-")) 

 

Brian Sung October 26, 2018

Still doesn't work. It breaks the macro if I try to access the values using either $var1.get(0) or $var1[0].

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 26, 2018

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.

Like # people like this
Brian Sung October 26, 2018

This worked. Thank you so much Bill!

Evgenii
Community Champion
October 7, 2022

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

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events