Concatenate text on NVelocity

There is no operator to concatenate two strings in NVelocity (the .Net version of Velocity) like we can do on other languages like C# or PHP, with the plus (+) operator for the first one and dot (.) for the second one. For example, in C# we would do:

string strHello = "Hello " + "World";

And in PHP:

$hello = "Hello " . "World";

But in NVelocity, what we must do if we need to do this same thing would be:

#set ($str1 = "Hello ")
#set ($str2 = "World")
The concatenated string would be: $str1$str2

Source: Velocity User Guide

3 thoughts on “Concatenate text on NVelocity”

  1. Well, that’s great, the $str1$str2 is perfectly rendered in a template. But in comparison to C# and PHP examples the expression #set ($str3 = $str1$str2)
    produces the parsing error:

    Encountered “$str2” at line XXXX, column YYY. Was expecting one of: … … “-” … “+” … “*” … “/” … “%” … “&&” … “||” … “<" … "” … “>=” … “==” … “!=” … … “=” …

    So, speaking about $str1$str2 we do not have the true string concatenation which produces the third string variable, despite $str1$str2 can be rendered correctly within a template as a sequence of two strings.

    Regards,
    Viktor Bober

  2. I’ve found in NVelocity help that you can do that type of concatenation, using the following sintax:

    #set( $size = “Big” )
    #set( $name = “Ben” )

    #set($clock = “$size$name” )

    The clock is $clock.

    Regards
    Charly

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *