Search code examples
javastringvelocityconfluence

Can't split at dot - Velocity


I would like to split a date at the dots between day.month.year. For example: 14.01.2015 to {14, 01, 2015} This is the code that i used: dates3.get(0) contains the String "14.01.2015" which i get from a textfield of the page.

##Splitting startingDate by point
#set($dates4 = [])
#foreach($id in $dates3.get(0).split(".")) ##BUG
#set($foo = $dates4.add($id))
$id<br>
#end

The array does not contain anything afterwards and when i print $id it just prints an empty line.

I figured that when i use - as a delimiter it works, but only for the month value. I have to put a - at the start and the end for it work (like this: "-14-01-2015-") and get the indexes 1-3 not 0-2, so that it works for all three values.


Solution

  • split() wants a regular expression (regex). The dot in a regex stands for "any character", so you need to escape it:

    .split("\.")
    

    (for the generic reader: in other contexts the backslash must me escaped by another backslash in order to survive the syntax of strings: .split("\\."))