如何在速度模板中使用 split()? [英] How to use split() in velocity template?

查看:32
本文介绍了如何在速度模板中使用 split()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在速度上下文中拆分一个字符串以获得一个数组作为回报,如下所示-

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???

推荐答案

使用 Velocity 1.7 和可能低于此的可以使用 String split() 方法来完成.与 Java 中的特殊字符不同,它不需要转义正斜杠(例如 "\\|").

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("[|]"))

注意 1:要获取数组的大小,请使用:$myArray.size()

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

注意 2:要获取实际值,请使用 $myArray.get(0)$myArray[0] ... etc

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

建议:可以提前使用 #if ($myString.indexOf(‘|’)) ... #end

这篇关于如何在速度模板中使用 split()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆