How to use split() in velocity template?

本小妞迷上赌 提交于 2021-02-09 11:33:49

问题


I am trying to split a string in velocity context to get an array in return like following--

   #if($stringValue.split("::")[1].length()==0)
       //some code

But it does not work for velocity.I am getting a parser error which is unable to compile []

So,how can I implement this logic in velocity???


回答1:


Using velocity 1.7 and possibly below this can be done using the String split() method. Unlike it's Java counterpart for special characters one doesn't need to escape the forward slash (.e.g "\\|").

#set ($myString = “This|is|my|dummy|text”) 

#set ($myArray = $myString.split("\|")) or
#set ($myArray = $myString.split('\|')) or
#set ($myArray = $myString.split("[|]"))

Note 1: To get the size of the array use: $myArray.size()

Note 2: To get actual values use $myArray.get(0) or $myArray[0] … etc

Suggestion: one could use beforehand #if ($myString.indexOf(‘|’)) ... #end



来源:https://stackoverflow.com/questions/11776820/how-to-use-split-in-velocity-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!