How to get the unicode of a character in Apache Velocity (VTL)

168 Views Asked by At

Im trying to obtain the Unicode(code) of a character in VTL. For example I would like to get the number 902 from the character Ά The analogue in JS would be:

'Ά'.charCodeAt(0)

902

Similarly the char code of a blank space would be 32:

' '.charCodeAt(0)

32

1

There are 1 best solutions below

0
On

What you need is the Character.codePointAt(str, index) static method.

#macro(unicode, $chr)
  #set($str = "$chr")
  $str.charAt(0).codePointAt($str, 0)
#end

#unicode('Ά')

Explanation: in VTL we need (dummy) instances to call static methods. We first convert the argument to a string and call charAt(0) on it to get a Character instance, just to be able to call the codePointAt() static method on its first character.

This seems rather convoluted, alas the Java API is not so great here.