使用Velocity拆分()将字符串分割成一个数组似乎没有工作 [英] Using velocity split() to split a string into an array doesnt seem to work

查看:7110
本文介绍了使用Velocity拆分()将字符串分割成一个数组似乎没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我恨速度,很少会使用它,但有时候,我在我的工作叫这样做。我永远无法真正弄清楚如何去使用它。

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.

我有这种

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

$ browseSiteProducts是一个数组。或列表。管他呢。我甚至不知道。产品ID的第一个警报工作正常。我得到|这是我所期待的,当打印出来。然后内环应该拆分对|作为分隔符,给我以内循环的警报。但是相反,我总是得到24警报,因为在产品ID 24个字符。所以拆分()不给我正确界定。到底我是什么做错了?

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

谢谢
凯尔

推荐答案

速度有极少的对象和它自己的方法。相反,它可以让你与真正的Java对象工作,并呼吁这些对象的真正的Java方法。这速度文档中说,分隔符是一个字符串?

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的,一个字符串只是一个可以容纳多种类型的信息的数据类型:电话号码,名称,标识,定期EX pressions ...在Java中,很多方法处理常规的前pressions这些信息传递的RE为字符串的对象。

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.

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

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

如果产品ID确实是 java.lang.String中 ,那么你可以检查的拆分方法需要一个字符串参数,但该字符串预计将是一个有效的常规前pression。

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.

和自 | 在常规的前pressions 特殊字符,你需要以某种方式逃避它。这工作:

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

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

这篇关于使用Velocity拆分()将字符串分割成一个数组似乎没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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