检查字符串是否包含Velocity中的特定子字符串

问题描述 投票:10回答:1

在Velocity中我有一个名为$ url的变量,它包含以下字符串:[ContentId(2.7507),ContentId(2.7508),ContentId(1.44551)]

我想检查该字符串是否包含子串1.44551。

这是我到目前为止编写的代码,但由于某种原因,它返回False:

#if($url.contains("1.44551"))
<p>The article content id is 1.44551</p>
#else
<p>The article content id isn't 1.44551</p>
#end

我希望这会返回True,因为$ url变量中存在1.44551子字符串。请有人告诉我哪里出错了?

java string substring velocity
1个回答
7
投票

Velocity将值保存为上下文映射中的对象。

如果要执行String方法,请添加toString以确保在String上执行contains方法:

 #if($url.toString().contains("1.44551"))
<p>The article content id is 1.44551</p>
#else
<p>The article content id isn't 1.44551</p>
#end
© www.soinside.com 2019 - 2024. All rights reserved.