FormatString

function FormatString(formatString: string, ...args: any[]): string | null

The function FormatString formats a given string by replacing placeholders in the string by the values of the symbols.

Parameter

Name

Type

Description

formatString

string

A string with placeholders.

…args [ Rest Parameter ]

any[]

Any value used in a placeholder.

Return value

Type

Description

string | null

String in which the placeholders are replaced by values of the symbols or null if anything other than string is passed as formatString.

Placeholder

Example: {0|.1f}

Placeholder

Description

{

Placeholder is initiated.

0

Index of the parameter (optional: without an index iteration takes place in sequence).

|

Separator.

+

If a + is specified here, the string is always assigned a sign. For example "+42" for the positive number 42. Negative numbers of course always get a sign.

.X

Number of digits.

f

Type description.

}

End of the placeholder.

Type description

Character

Description

d

Signed decimal integer

i

Signed decimal integer

u

Unsigned decimal integer

o

Unsigned octal

x

Unsigned hexadecimal integer

X

Unsigned hexadecimal integer (upper case)

f

Decimal floating point

e

Scientific notation (mantissa/exponent), lower case

g

Use the shortest representation: e or f

s

String of characters

b

Binary number

t

true or false

T

Type of the argument ('number', 'string', 'null' …)

v

Primitive value of the specified argument

j

JavaScript object or array as a JSON encoded string

NuGet package: Beckhoff.TwinCAT.HMI.Functions

FormatString 1:

NuGet packages are available from version 1.12.

FormatString 2:

Available from version 1.10

Sample

Input:

FormatString('Hello {1|.5s}. This is a floating point number - {0|.2f}. Two zeros are attached - {03i} (3 digits in total).', 2, 'World123', 3);

Output:

"Hello World. This is a floating point number – 2.00. Two zeros are attached – 003 (3 digits in total)."