Output part of a string in velocity

孤街醉人 提交于 2020-01-11 04:37:51

问题


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").

I want to output all text after the 3 Asterisks. I've scoured the forums but cant quite find anything that helps me completely.


回答1:


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))

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.



来源:https://stackoverflow.com/questions/12477518/output-part-of-a-string-in-velocity

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