String Concatenation Operator

The string concatenation operator is the ampersand symbol (&). Use the ampersand (&) operator to concatenate strings or binaries; for example:

greeting(name: String): String;
begin
    return "Hello " & name & ". How are you?";
end;

Using this example, execute the following code.

write greeting("Ricky Alfonso");

The execution of this code displays the following in the Jade Interpreter Output Viewer window.

Hello Ricky Alfonso. How are you?

A concatenating operation involving strings, binary values, and UTF‑8 strings is optimized in Jade when the values being concatenated are local variables, constants, or literals. In this case, the Jade interpreter determines the size of the resulting string, allocates a single block in the string pool for the result, and constructs the concatenated value directly into this block.

If the values being concatenated are properties or the results of method calls, the operation is more complex and multiple blocks are allocated from the string pool. As allocating blocks is a relatively expensive operation, avoiding this significantly improves the elapsed time of a concatenation operation. The following example shows non-recommended and recommended coding techniques.

vars
    prod : Product;
    xml : String;
    code : String;
    desc : String;
begin
    // Relatively inefficient
    foreach prod in products do
        xml := xml & "<product><code>" & prod.code & "</code><desc>" &
                                         prod.desc & "</desc></product>";
    endforeach;
    // Using local variables is much more efficient
    foreach prod in products do
        code := prod.code;
        desc := prod.desc;
        xml := xml & "<product><code>" & code & "</code><desc>"
                                       & desc & "</desc></product>";
    endforeach;
end;