用Velocity模板语言替换字符串的子串

kis*_*ore 32 string velocity replace vtl

我想用另一个字符串替换Velocity Template Language中的一部分字符串.

例如:

#set($a = "Hello")
#set($b = "+")
Run Code Online (Sandbox Code Playgroud)

我想用Hello替换ll中的ll.输出应该是He ++ o

请帮我

谢谢基肖尔

Mar*_*ren 59

默认情况下,您可以使用Java String对象的方法:

#set( $a = "Hello" )
#set( $b = $a.replace("l", "+") )
${b}
Run Code Online (Sandbox Code Playgroud)

将生成He ++ o并且您还可以使用velocity变量作为方法调用的参数,例如:

#set( $a = "Hello" )
#set( $b = "+" )
#set( $c = $a.replace("l", ${b}) )
${c}
Run Code Online (Sandbox Code Playgroud)