使用velocity split()将字符串拆分为数组似乎不可行。

时间:2022-11-24 18:39:07

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

我有这个

#foreach( $product in $browseSiteProducts )
    alert("$product.productId");
    #foreach( $stringList in $product.productId.split("|") )
        alert("inner loop");
    #end
#end

$browseSiteProducts is an Array. Or List. Or whatever. I don't even know. The first alert of the productId works fine. I get "|" which is what I expected when printed out. The inner loop then should split that on the "|" as the delimiter and give me to alerts of "inner loop". But instead I always get 24 alerts because there are 24 characters in the productId. so split() is not delimiting correctly for me. What the heck am I doing wrong??

browseSiteProducts美元是一个数组。或列表。之类的。我甚至不知道。该产品的第一个警报工作正常。我得到了|,这是我打印出来时所期望的。然后内部循环将其拆分为“|”作为分隔符,并将其通知“内部循环”。但是我总是得到24个警告,因为productId中有24个字符。所以split()并没有对我进行正确的分隔。我到底做错了什么?

Thanks Kyle

由于凯尔

4 个解决方案

#1


11  

Velocity has extremely few objects and methods of its own. Instead, it allows you to work with real Java objects and call real Java methods on those objects. Which Velocity documentation says that the delimiter is a string?

速度本身很少有对象和方法。相反,它允许您使用真正的Java对象,并在这些对象上调用实际的Java方法。哪个速度文档说分隔符是字符串?

Moreover, since Velocity is Java-based, a string is just a data type that can hold many types of information: phone numbers, names, identifiers, regular expressions... In Java, many methods dealing with regular expressions pass those REs as String objects.

此外,由于Velocity是基于java的,所以字符串只是一种数据类型,它可以包含多种类型的信息:电话号码、姓名、标识符、正则表达式……在Java中,处理正则表达式的许多方法都将其作为字符串对象传递。

You can check the actual type that a value behind a variable has by printing its classname:

您可以通过打印变量的类名来检查变量后面的值的实际类型:

Product class is $product.class
Product ID class is $product.productId.class

If the product ID is indeed a java.lang.String, then you can check that the split method takes a String parameter, but that String is expected to be a valid regular expression.

如果产品ID确实是java.lang。然后,您可以检查split方法是否接受一个字符串参数,但是该字符串应该是一个有效的正则表达式。

And since | is a special character in regular expressions, you need to escape it somehow. This works:

由于|是正则表达式中的一个特殊字符,您需要以某种方式转义它。如此:

#foreach( $stringList in $product.productId.split("[|]") )

#2


4  

Without using StringUtils this can be done using the String split() method.

不使用StringUtils,可以使用String split()方法来完成。

Unlike it's Java counterpart for special characters one doesn't need to escape the forward slash (.e.g "\\|") as @kamcknig correctly stated:

与Java对应的特殊字符不同,不需要转义斜杠(.e)。g“只\|”)@kamcknig正确声明:

#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()

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

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

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

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

建议:可以事先使用#if ($ string . indexof(' | '))……#结束

#3


1  

## Splitting by Pipes
#set($prodId = $product.productId)
#foreach($id in $prodId.split("[|]"))
$id
#end

#4


-1  

Apparently even though the velocity documentation says that the delimiter is a string it's really a regular expression. Thanks apache.

显然,尽管velocity文档说分隔符是一个字符串,但它实际上是一个正则表达式。由于apache。

proper way is

正确的方法是

$product.product.split("\|");

#1


11  

Velocity has extremely few objects and methods of its own. Instead, it allows you to work with real Java objects and call real Java methods on those objects. Which Velocity documentation says that the delimiter is a string?

速度本身很少有对象和方法。相反,它允许您使用真正的Java对象,并在这些对象上调用实际的Java方法。哪个速度文档说分隔符是字符串?

Moreover, since Velocity is Java-based, a string is just a data type that can hold many types of information: phone numbers, names, identifiers, regular expressions... In Java, many methods dealing with regular expressions pass those REs as String objects.

此外,由于Velocity是基于java的,所以字符串只是一种数据类型,它可以包含多种类型的信息:电话号码、姓名、标识符、正则表达式……在Java中,处理正则表达式的许多方法都将其作为字符串对象传递。

You can check the actual type that a value behind a variable has by printing its classname:

您可以通过打印变量的类名来检查变量后面的值的实际类型:

Product class is $product.class
Product ID class is $product.productId.class

If the product ID is indeed a java.lang.String, then you can check that the split method takes a String parameter, but that String is expected to be a valid regular expression.

如果产品ID确实是java.lang。然后,您可以检查split方法是否接受一个字符串参数,但是该字符串应该是一个有效的正则表达式。

And since | is a special character in regular expressions, you need to escape it somehow. This works:

由于|是正则表达式中的一个特殊字符,您需要以某种方式转义它。如此:

#foreach( $stringList in $product.productId.split("[|]") )

#2


4  

Without using StringUtils this can be done using the String split() method.

不使用StringUtils,可以使用String split()方法来完成。

Unlike it's Java counterpart for special characters one doesn't need to escape the forward slash (.e.g "\\|") as @kamcknig correctly stated:

与Java对应的特殊字符不同,不需要转义斜杠(.e)。g“只\|”)@kamcknig正确声明:

#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()

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

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

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

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

建议:可以事先使用#if ($ string . indexof(' | '))……#结束

#3


1  

## Splitting by Pipes
#set($prodId = $product.productId)
#foreach($id in $prodId.split("[|]"))
$id
#end

#4


-1  

Apparently even though the velocity documentation says that the delimiter is a string it's really a regular expression. Thanks apache.

显然,尽管velocity文档说分隔符是一个字符串,但它实际上是一个正则表达式。由于apache。

proper way is

正确的方法是

$product.product.split("\|");