Replace a Substring of a String in Velocity Template Language

巧了我就是萌 提交于 2019-12-03 10:34:20

问题


I want to replace a part of a string in Velocity Template Language with another string.

For Example:

#set($a = "Hello")
#set($b = "+")

I want to replace ll in Hello with ++. The output should be He++o

Please help me

Thanks Kishore


回答1:


By default you can use the methods of the Java String object:

#set( $a = "Hello" )
#set( $b = $a.replace("l", "+") )
${b}

will produce He++o and you can also use velocity variables as arguments to your method calls, e.g.:

#set( $a = "Hello" )
#set( $b = "+" )
#set( $c = $a.replace("l", ${b}) )
${c}


来源:https://stackoverflow.com/questions/6843882/replace-a-substring-of-a-string-in-velocity-template-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!