Using velocity split() to split a string into an array doesnt seem to work

前端 未结 5 1733
猫巷女王i
猫巷女王i 2021-01-11 12:17

I HATE velocity and rarely ever use it but sometimes I am called upon at my job to do so. I can never really figure out just how to use it.

I have this



        
5条回答
  •  北海茫月
    2021-01-11 12:58

    Without using StringUtils 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 "\\|") as @kamcknig correctly stated:

    #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

提交回复
热议问题