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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6843882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:10:57  来源:igfitidea点击:

Replace a Substring of a String in Velocity Template Language

stringreplacevelocityvtl

提问by kishore

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

我想用另一个字符串替换 Velocity 模板语言中字符串的一部分。

For Example:

例如:

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

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

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

Please help me

请帮我

Thanks Kishore

谢谢基肖尔

回答by Mark McLaren

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

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

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

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

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

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