如何获得速度中 "prüfen" 的长度

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

我试图获得 "prüfen" 的速度长度。

示例:

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

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

$total_length  // Output is 6

问题:我希望输出为 7(因为字符“ü”占 2 的大小) 如何在 velocity.

中获取 "prüfen" 的长度

我也试过

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

$len_of_data

但没有任何效果。

这似乎有效:

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

请务必使用 input.encoding = UTF-8 配置 Velocity。

使用 Velocity 1.7+,您可以像调用列表一样调用数组 size()(以及 isEmpty()get(int))。