Duxa
Duxa

Reputation: 75

How get first element of string array in jira velocity?

I'm new in velocity and need help. I have com.atlassian.jira.user.RemoteUser and remoteUser.getDisplayName() returns string which I want to split around matches of the given regular expression. And then get first element of String array, but unfortunately I have:

#set ($userName = $remoteUser.getDisplayName().split(" "))

$userName, Hello!

And have [Ljava.lang.String;@74a70acb as I can understand it is String array.

But if I try try get
#set ($userName = $remoteUser.getDisplayName().split(" ")[0])

$userName, Hello!

I have

org.apache.velocity.exception.ParseErrorException: Encountered "[" at getEncodedBodyFromContent[line 1, column 58] Was expecting one of: <RPAREN> ... <WHITESPACE> ... "-" ... "+" ... "*" ... "/" ... "%" ... <LOGICAL_AND> ... <LOGICAL_OR> ... <LOGICAL_LT> ... <LOGICAL_LE> ... <LOGICAL_GT> ... <LOGICAL_GE> ... <LOGICAL_EQUALS> ... <LOGICAL_NOT_EQUALS> ... <DOT> ...

If I try #set ($userName = $remoteUser.getDisplayName().split(" ")).get(0)) then I get null pointer exception

Upvotes: 1

Views: 641

Answers (2)

Duxa
Duxa

Reputation: 75

I don't know why but $string.split(" ") command doesn't work. After that I thought I had String array, but after $userName = $remoteUser.getDisplayName().split(" ").get($index) I had null pointer exception.
Solution which helped was using indexOf() and substring() commands like this:

#set ($userName = $remoteUser.getDisplayName())
#set ($index = $userName.indexOf(' '))
#set ($surname = $userName.substring(0, $index))
#set ($index = $index + 1)
#set ($name = $userName.substring($index))

Upvotes: 0

Claude Brisson
Claude Brisson

Reputation: 4130

Velocity 1.6.4 is more than ten years old and doesn't support arrays.

Velocity 1.7 was released in November 2010 and does supports arrays.

Someone, somewhere, should upgrade something (FYI current version is Velocity 2.2 ...).

Upvotes: 0

Related Questions