Replace a Substring of a String in Velocity Template Language

前端 未结 1 1845
慢半拍i
慢半拍i 2021-02-05 01:17

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

For Example:

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


        
1条回答
  •  囚心锁ツ
    2021-02-05 01:43

    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}
    

    0 讨论(0)
提交回复
热议问题