<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > BuiltIn Functions > String functions > $() String Interpolation |
Description
The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results.
Syntax
s = $(StringExpression [, ActivateRuntimeErrors, [sBlockLeftDelimiter, sBlockRightDelimiter] ])
Returns
String
If an error will occur during any expression evaluation, an empty string will be returned
Parameters
Name |
Type |
Optional |
Meaning |
StringExpression |
String |
No |
A string expression containing one or more interpolated strings to be interpreted as thinBasic string expression. Interpolated string must be included between { and } |
ActivateRuntimeErrors |
Number |
Yes |
By default $() will ignore run-time errors while interpreting interpolated expressions.
Setting this optional parameter to %TRUE will generate a parsing error if one interpolated expression will generate e run-time error. |
sBlockLeftDelimiter |
String |
Yes |
By default left delimiter for interpolated strings is { You can change it passing your preferred delimiter |
sBlockRightDelimiter |
String |
Yes |
By default right delimiter for interpolated strings is } You can change it passing your preferred delimiter |
Remarks
The structure of an item with an interpolation expression is as follows: {interpolationExpression}
That is: a valid thinBasic expression between { and } delimiters.
{ and } delimiters can be changed passing personalized delimiters to $ function
Restrictions
{interpolationExpression} must contain a valid thinBasic expression in the running context and scope.
It can refer to local or global variables.
It can contains string and numeric expressions
It can contains any valid module functions active in current script.
IMPORTANT:
{interpolationExpression} is always interpreted as a string expression following all thinBasic rules of string expressions.
For example + sign is directly considered string concatenation and not sum of two expressions.
If you want execute math calculations include them into Str$() or Format$() or any other function that will return a string from a math expression
Example:
this will return "Value is 11"
printl $("Value is {1 + 1}")
this will return "Value is 2"
printl $("Value is {Format$(1 + 1)}")
IMPORTANT:
{interpolationExpression} cannot be recursive: it cannot contains another interpolation expression.
But StringExpression parameter can contains up to 1024 {interpolationExpression} expressions.
See also
Examples
uses "console"
long X = 2
long Y = 3
'--- String interpolation:
printl $("The point ""{X}, {Y}"" is {Sqr(X * X + Y * Y)} from the origin")
'--- output: The point "2, 3" is 3.60555127546398929 from the origin.
WaitKey
'-----------------------------------------------------------------------------------
PrintL
string sName = "<AnyName>"
dim lDate as new CDATETIME
'--- String interpolation:
printl $("Hello, {sName}! Today is {ldate.DateStringLong}, it's {ldate.TimeString24} now.")
WaitKey