BRATVADDI
BRATVADDI

Reputation: 199

Velocity template substring issue

I have an issue with extracting a substring in velocity. the string I have is 1M/1Y (the variable string here) I need to extract 1M and 1Y. what is the best way to do it?

#set($index=$string.index('/'))
#set($val=$string.substring($index,index+2))

what am I doing wrong here?

Upvotes: 7

Views: 23901

Answers (3)

Thien Nguyen
Thien Nguyen

Reputation: 41

You missed $ before the last 'index' variable, this should fix your code:

#set($index=$string.index('/'))
#set($val=$string.substring($index,$index+2))

Upvotes: 0

stuzocub
stuzocub

Reputation: 21

You can use stringUtil:

#set($parts = $stringUtil.split($string, "/"))
$parts.get(1)
$parts.get(2)
....

Upvotes: 1

pushpavanthar
pushpavanthar

Reputation: 869

In velocity template we have access to all the public methods of the String class. Try using the below code

#set ($index = $string.indexOf('/'))
#set ($val1= $string.substring(0, $index))
#set ($index = $index + 1)
#set ($val2 = $string.substring($index))

or you can also make use of $string.split("/") if you are using Velocity 1.7

Upvotes: 15

Related Questions