Skip to content
QiTASC.com / General Concepts /
String Interpolation
/ .. /
String Interpolation





String Interpolation

Interpolated Strings (also called Template Strings) contain Evaluation Groups, or eval groups, whose content is interpreted. A Template String always results in a String. The main purpose for Template Strings is to simplify the output of formatted strings. However, Template Strings can also be used to perform String arithmetics. Because Template Strings are common Steps Language strings, they can be enclosed in double quotes ("") or single quotes ('').

Notes about String Interpolation Usage

  • Every String that is used within the Steps Language can be a Template String

  • An eval group starts with ${ and ends with a curly bracket, }

  • Everything placed within an eval group is passed on to the interpreter for evaluation

An eval group may contain everything that is eligible for a right-hand side of an assignment. Therefore, variables and functions can be used. Additionally:

  • Multiple eval groups can be used in a Template String

  • Nesting eval groups should be avoided, because they would reduce the readability of the code

  • Whenever a Template String does not contain any eval groups, it evaluates to a standard Steps Language String

Configuration

The interpolateStrings field of the Language configuration interpolates Strings when set to true. If set to false, Strings will not be treated as Template Strings even if they contain eval groups. The default for interpolateStrings is false to avoid any backward incompatibilities.

More details about the Language configuration is available in String Escaping and Interpolation in Custom Languages.

Configuration Example

1
2
3
4
5
Language {
interpretStringEscapes : true,
  interpolateStrings : true,
  interpolateWebtestDefinitions : true
}

Basic Interpolation Examples

'${2+3}'

In '${2+3}', the Template String holds a single eval group that contains 2+3. Evaluating this simple Expression yields 5, which is returned as a String.

Example

1
'${2+3} seconds after midnight'

The example above yields 5 seconds after midnight:

  • The content of this String outside the eval group is not interpreted: Instead, it is treated as conventional String content

  • The eval group's content, 2+3, evaluates to the String 5, which is placed right into the surrounding content to yield the final result

'${x} world'

In '${x} world', the eval group's content is a variable reference that is evaluated against the context.

Example

1
'${x} world'
If the context contains a String variable x with the content "hello", the result is the String hello world:

  • An eval group may contain everything that is eligible for a right-hand side of an assignment. Therefore, variables and functions can be used.

Advanced String Interpolation Examples

'within ${x+2} seconds, ${a} ends the call'

In 'within ${x+2} seconds, ${a} ends the call', the Template String contains 2 eval groups within a Template String. Both are evaluated using the context that holds assignments of x to 6 and a to "A".

Example

1
'within ${x+2} seconds, ${a} ends the call' yields within 8 seconds, A ends the call

The example above yields within 8 seconds, A ends the call:

  • Multiple eval groups can be used in a template string. However, nesting of eval groups, is not allowed.

'"${a}" is here'

In '"${a}" is here' the eval group is placed within double quotes ("").

Example

1
'"${a}" is here'

The example above yields "Batman" is here:

  • Double quotes ("") are not treated specially within Template Strings enclosed in single quotes ('')

  • Because the Template String is enclosed within single quotes, double quotes can be used like conventional characters inside the Template String

  • If the context holds a variable a with the content Batman, the result is obvious

  • This example can be transposed by delimiting the Template String with double quotes and using single quotes around the eval group yielding 'Batman' is here

Additional Information about Curly Brackets

Matching Curly Brackets - General

Curly brackets {} within an eval group must be matched. Otherwise, parsing the Template String fails. If the eval group contains a String, the String may contain unmatched curly brackets (if they are outside an eval group).

Example

1
${"the one and only " + a} is here

The example above yields the one and only Batman is here.

Matching Curly Brackets - Outside of Inner String

Curly brackets must be matched if they are not placed within an inner String. Outside if eval groups, curly brackets do not need to be matched.

Example 1

1
'${"he"}llo}'"

The example above yields hello}:

  • The first closing curly bracket is interpreted as the end of the eval group

  • The second closing curly bracket is outside of the eval group and does not need to be matched

Example 2

1
'${"he"}}}'

The example above returns he}}:

  • The two last closing curly brackets are interpreted as usual String characters.

Unmatched Curly Brackets

An unmatched curly bracket may be used inside the string is possible if the unmatched bracket is outside an eval group as seen from the String's scope:

Example

1
'${"hel{lo"}'

The example above yields hel{lo.