检查字符串是否包含 Velocity 中的特定子字符串 [英] Checking if a string contains a particular substring in Velocity

查看:60
本文介绍了检查字符串是否包含 Velocity 中的特定子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Velocity I have a variable called $url which contains the following string: [ContentId(2.7507), ContentId(2.7508), ContentId(1.44551)]

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

I want to check if that string contains the substring 1.44551.

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

This is the code I've written so far, but for some reason it's returning 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 子字符串.请有人告诉我我哪里出错了?

I would expect this to be returning True, as the 1.44551 substring is present in the $url variable. Please can someone tell me where I'm going wrong?

推荐答案

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

Velocity holds values as Objects in context map.

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

If you want to execute String method, add toString to make sure you execute on String the contains method:

 #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

这篇关于检查字符串是否包含 Velocity 中的特定子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆