简体   繁体   中英

split a string in velocity Template and access it by index in velocity

I am trying to split a string in velocity using split() given below:

#foreach ($element in $string.split(":"))
    #if($velocityCount >1)
    #foreach ($var in $element.split(","))
    #if($velocityCount ==1)
    @Param("$var") String $var,
#end
#end
#end
#end

I want to store the splitted string in an array and want to access it by index as we do in java.

String s[]=str.split(".");
s[0];

Is there anything similar to above java code in velocity?

I am pasting from the velocity user guide :

All array references are treated as if they are fixed-length lists. This means that you can call java.util.List methods and properties on array references.

So in your case, to get the first element of the array after you split the string, you could just do:

#set ($s = $string.split(":"))
$s.get(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM