Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a string contains a particular substring in Velocity

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

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

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

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?

like image 425
Victoria Avatar asked Nov 05 '12 09:11

Victoria


People also ask

How do you check if a string contains a specific substring?

The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the substring.

How do I get substring in Velocity template?

You can use stringUtil: #set($parts = $stringUtil. split($string, "/")) $parts.

How do you check if a string contains a particular substring in Java?

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

What is Velocity .VM file?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.


1 Answers

Velocity holds values as Objects in context map.

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
like image 146
user7294900 Avatar answered Sep 19 '22 14:09

user7294900