$() String Interpolation

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Language > thinBasic 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 = $(StringExpresion [, ActivateRuntimeErrors])

 

Returns

 

String

 

Parameters

 

Name

Type

Optional

Meaning

StringExpresion

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.

 

Remarks

 

The structure of an item with an interpolation expression is as follows:

{interpolationExpression}

 

Restrictions

 

{interpolationExpression} must be 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.

 

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