Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the length of "prüfen" in velocity

Tags:

velocity

I was trying to get the length of "prüfen" in velocity.

Example:

#set ($data = "prüfen")

#set( $total_length = $data.length())

$total_length  // Output is 6

Question : I want output as 7 (as character "ü" occupies size of 2) How to get the length of "prüfen" in velocity.

I have also tried with

#set( $len_of_data = $data.getBytes().length)

$len_of_data

But nothing is working.

like image 298
Shanky Avatar asked Dec 04 '25 08:12

Shanky


1 Answers

This seems to work:

#set ($data = "prüfen")
#set( $total_length = $data.getBytes('UTF-8').size())
$total_length

Be sure to configure Velocity with input.encoding = UTF-8.

With Velocity 1.7+, you can call size() on an array as if it was a list (as well as isEmpty() and get(int)).

like image 86
Claude Brisson Avatar answered Dec 06 '25 13:12

Claude Brisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!