问题
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