String operations

Interpolation or concatenation

To insert the value of an expression into a string, you can use ${...} and #{...} in string literals. ${...} behaves similarly as in text sections. For example, assuming that user is “Big Joe”:

${"Hello ${user}!"}
${"${user}${user}${user}${user}"}  

produces this output:

Hello Big Joe!
Big JoeBig JoeBig JoeBig Joe  

Alternatively, you can use the + operator to achieve the same result. This method is called string concatenation. It is the preferred method. For example:

${"Hello " + user + "!"}
${user + user + user + user}  

This will print the same as the example with the ${...}.

IMPORTANT: Interpolations work only in text sections (e.g. <h1>Hello ${name}!</h1>) and in string literals (e.g. <#include "/footer/${company}.html">). A common mistake is using <#if ${isBig}>Wow!</#if>, which is syntactically incorrect. Instead, write <#if isBig>Wow!</#if>. <#if "${isBig}">Wow!</#if> is incorrect as well, since the parameter value will be a string, and the if directive requires a boolean value, so it will cause a runtime error.

Getting a character

You can get a single character of a string at a given index in the same way as you read the sub-variable of a sequence, e.g. user[0]. The result is a string whose length is 1; RPL does not have a separate character type. As with sequence sub-variables, the index must be a number that is at least 0 and less than the length of the string. Otherwise, an error will terminate template processing.

Since the sequence sub-variable syntax and the character getter syntax clashes, you can use the character syntax only if the variable is not a sequence as well, since in that case the sequence behavior prevails. As a workaround, you can use the string built-in, e.g. user?string[0].

For example, assuming that user is “Big Joe”:

${user[0]}
${user[2]}  

produces this output (note that the index of the first character is 0):

B
g

Next steps

Sequence operations

Hash operations