substring() in User Macro?

I d like to e.g. make version numbers dynamic dependent on space.

Currently they are only semi-dynamic with

#if ($space.key=="MY55")5.5#elseif ($space.key=="MY50")5#else <MacroError_VersionNotDefined>#end

With a substring() func this could be dynamic!
Takers?
Thanks
G.

3 answers

1 accepted

This widget could not be displayed.

As I neither get a professional answer here nor on the Velocity mailing list (at least not yet and you are bugging me to close the case) the workaround I see is via a variable;

## @noparams
#set($len = $space.key.length() - 2)
$space.key.substring($len)

This widget could not be displayed.

User macros are coded in Velocity, that means both velocity specific and java methods will work.

#set($ver = $spaceKey.substring($spaceKey.length-2,2))

$ver

will be 55 or 50 depending.

See more methods to manipulate a Java String http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

This widget could not be displayed.

Thanks, Eddie
for the pointers to Velocity and Java.
They helped me iron out most kinks.
Here is code that actually works:

## @noparams
#set($len = $space.key.length() - 2)
\$space.key.length(): $len <br>
\$space.key.substring(\$len): $space.Key.substring($len)

It contains some nice Velocity features like escaped $s, etc.

What cost me a lot of time is

$space.Key.substring($space.key.length() - 2)

I have no idea why this construct doesn t work, while the variable as argument does.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in Teamwork

What teamwork quotes inspire you?

Hey everyone! My name is Natalie and I'm an editor of the Atlassian Blog and I've got a question for you: What's your favorite quote about teamwork?  We've compiled a list here, along with...

140 views 16 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you