Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 "<stuff>|<morestuff>" 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??

Thanks Kyle

like image 309
kamcknig Avatar asked Jan 22 '14 16:01

kamcknig


People also ask

How do you split a string into an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Does split work on array?

The split( ) method doesn't work directly for arrays. However, we can first convert the elements of our array to a string, then we can use the split( ) method.

How do you split a string into an array in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Can you split a string array in Java?

Split() String method in Java with examples. The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.


2 Answers

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?

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

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.

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

#foreach( $stringList in $product.productId.split("[|]") )
like image 197
Sergiu Dumitriu Avatar answered Sep 20 '22 02:09

Sergiu Dumitriu


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

like image 43
Ithar Avatar answered Sep 20 '22 02:09

Ithar