Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity template substring issue

Tags:

java

velocity

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?

like image 270
BRATVADDI Avatar asked Sep 06 '16 17:09

BRATVADDI


People also ask

How do I get substring in Velocity template?

You can use stringUtil: #set($parts = $stringUtil. split($string, "/")) $parts.

What is .VM file in Java?

Developer file used by Velocity, a Java-based template engine; written using the Velocity Template Language (VTL); contains VTL statements inserted in a normal text document; often used for auto-generating Web source code and class skeletons.

What is Apache Velocity template?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.


1 Answers

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

like image 115
pushpavanthar Avatar answered Sep 18 '22 22:09

pushpavanthar