简体   繁体   English

在速度模板中分割字符串,并通过速度索引访问它

[英]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: 我正在尝试使用下面给出的split()在速度上分割字符串:

#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. 我想将拆分后的字符串存储在数组中,并希望像在Java中一样通过索引访问它。

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

Is there anything similar to above java code in velocity? 在速度上有什么类似于上面的Java代码的东西吗?

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. 这意味着您可以在数组引用上调用java.util.List方法和属性。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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