22

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?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Victoria
  • 901
  • 3
  • 15
  • 30
  • Looks fine at first glance. If you write your test as `#if(true)` or `#if($url.equals($url))`: does it return True then? I guess what I'm getting at is, make sure that Velocity is evaluating the expression. – Evan Haas Nov 05 '12 at 13:48
  • 2
    oops, the same thing was happening for me (but I realised the object I was trying to use contains on wasn't a String!)... once I called toString first it was fine i.e. `messageQuery.message.JMSDestination.toString().contains("DLQ")` – Wayne Earnshaw Jun 17 '13 at 00:56

1 Answers1

21

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
Ori Marko
  • 56,308
  • 23
  • 131
  • 233