SOLVED

Velocity "Contains" operator case sensitive?

Go to solution
Highlighted
Level 6 - Champion

Velocity "Contains" operator case sensitive?

Is there a way to write a "contains" command in velocity that is NOT case sensitive? For example:

#if !$lead.Variable.contains("Value")

Output

#else

Default

#end

In that logic, the contains "Value" is case sensitive. Any way to write the command to bypass case sensitivity? I've tried a few variations with no luck.

Thanks,

Chris

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 6 - Champion

Re: Velocity "Contains" operator case sensitive?

Hey Sanford,

I was chatting with someone else about this and they offered the solution of using the matches function and some regex instead of "contains." Specifically, using:

$lead.Variable__c.matches(".*(?i:value).*")

In all my testing, appears to work exactly as expected. Any thoughts on this? I've never used regex in velocity and wasn't sure if there's a reason this approach isn't a good option.


Appreciate your input!
Chris

View solution in original post

8 REPLIES 8
Highlighted
Level 5

Re: Velocity "Contains" operator case sensitive?

Usually, I just lowercase it before the #if block. This came about because I didn't want to make rules for: Value, value, VALUE, VaLuE etc.

hope it helps!

Highlighted
Level 6 - Champion

Re: Velocity "Contains" operator case sensitive?

Ok that could help! So you take all of the variables you're going to use in the logic, set them as lowercase, then reference the #set value in your contains? Or is there another way to do lowercasing that you're mentioning.

Thank you very much!

Highlighted
Level 5

Re: Velocity "Contains" operator case sensitive?

Yes, so long as your use case is fairly simple, that works:

##toLowerCase example

#set( $var = $fieldName__c.toLowerCase() )

#if( $var == "pickup" )

##do work

#elseif( $var == "delivery" )

##do other work

#end

##toLowerCase example

#if( $fieldName__c.toLowerCase() == "pickup" )

##do work

#elseif( $fieldName__c.toLowerCase() == "delivery" )

##do other work

#end

If you have a lot of potential values, you might use a list to make it more scalable.

Note: I vaguely remember an issue where: $var.toLowerCase() may have limitations if you are sending emails in a foreign language such as French although I'm not 100% certain as my current instance is English only. Just wanted to mention it may be worth a test if needed for you.

Highlighted
Level 10 - Community Moderator

Re: Velocity "Contains" operator case sensitive?

Mark's recommendation of toLowerCase()-ing both sides is, unfortunately, the best we've got within Marketo's Velocity deployment.

toLowerCase() is not 100% accurate across the Unicode range (in Greek, for example) and perhaps more important, is wildly inefficient for long strings (for reasons that should be obvious when you think about it ). A compiled regular expression is better on the efficiency side (though not the accuracy side) but compiling is complex in Velocity so best to skip that.

There used to be another way to do this that's both accurate and fast... but with some upcoming changes, that way will no longer work in Marketo.

What exactly are you trying to do where you need to match string-in-string? There may be a better way.

Highlighted
Level 6 - Champion

Re: Velocity "Contains" operator case sensitive?

Hey Sanford,

I was chatting with someone else about this and they offered the solution of using the matches function and some regex instead of "contains." Specifically, using:

$lead.Variable__c.matches(".*(?i:value).*")

In all my testing, appears to work exactly as expected. Any thoughts on this? I've never used regex in velocity and wasn't sure if there's a reason this approach isn't a good option.


Appreciate your input!
Chris

View solution in original post

Highlighted
Level 10 - Community Moderator

Re: Velocity "Contains" operator case sensitive?

An uncompiled regex is significantly slower with no increase in accuracy. It also takes more memory (though you wouldn't notice until the haystacks are 1000+ characters).

If you could compile the regex (Pattern.compile), or better yet use compiled Java code that uses regionMatches, that would be superior to lowerCase. But we don't have those luxuries in Velocity.

The regex String.matches is harmless for small strings and I might well use it myself for readability (important in Velocity!) but we need to be aware it's not best design-wise.

Highlighted
Level 6 - Champion

Re: Velocity "Contains" operator case sensitive?

Thanks Sanford, we're searching small strings for essentially placeholder test for suppression purposes so I think we'll be fine from a length perspective. Really appreciate your opinion!

Highlighted
Level 10 - Community Moderator

Re: Velocity "Contains" operator case sensitive?

Not just an opinion, I've been profiling JVM resource utilization w/Velocity the past few days.