以速度输出字符串的一部分 [英] Output part of a string in velocity
问题描述
抱歉,如果我胡言乱语或胡言乱语,但我是速度和这些论坛的新手!
apologies if I waffle or talk a bit of jibberish but I'm new to velocity, and these forums!
我需要检查某个字符的字符串内容,如果出现,则输出文本的第二部分.例如:
I need to check the contents of a string for a certain character and output the second part of the text if it appears. For example:
set ($string = "This is a long string *** but I only want to output this on my email").
我想输出 3 个星号后的所有文本.我已经搜索了论坛,但找不到任何可以完全帮助我的东西.
I want to output all text after the 3 Asterisks. I've scoured the forums but cant quite find anything that helps me completely.
推荐答案
Velocity 只是真实 Java 对象的外观,因此您可以访问所有 String
类 的公共方法,包括 indexOf
和 substring
.因此,请尝试以下操作:
Velocity is just a façade for real Java objects, so you have access to all the public methods of the String
class, including indexOf
and substring
. So try something like:
#set ($string = "This is a long string *** but I only want to output this on my email")
#set ($index = $string.indexOf('***'))
#set ($index = $index + 3)
#set ($end = $string.substring($index))
如果您对放入上下文的对象有更多控制权,您可以添加一个 StringUtils
作为辅助工具,然后使用 substringAfter
直接.
If you have more control over what objects you put in the context, you could add an instance of StringUtils
as a helper tool, and then use substringAfter
directly.
这篇关于以速度输出字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!