Sprintf


The global Sprintf function formats text to a string and returns the string to the script. This provides functionality similar to the sprintf function in the C language.

Syntax

sString = Sprintf( sFmt, ... )

bullet.gif    sFmt is a format string.

bullet.gif    ... refers to an optional variable argument list (see Remarks).

bullet.gif    sString is the returned format string. It may be passed directly to a printing function.

bullet.gif    On success, this function returns true.

bullet.gif    On failure, this function returns false.

Remarks

Sprintf takes 1 or more arguments. The first argument is a format string and is required. Additional arguments specify expressions needed by the fields embedded in the format string. This function is similar to the sprintf function in C (for details about using the format string, refer to documentation for sprintf in the C or C++ language). The difference between these two implementations is that the C sprintf function takes the target string as its first argument. In contrast, the present function returns a string but does not use it as an argument.

Often it is desirable to use Sprintf directly without storing the returned string. To do this, specify it as the argument of a function like Printf or CFile:Printf in the following way:

     Printf( Sprintf( fmt, arg2, arg3, etc. ) )

This is equivalent to the following 2-line procedure:

     s = Sprintf( fmt, arg2, arg3, etc. )

     Printf( s )

You can also use the format string to explicitly create embedded formats using the%% construct to realize a % sign in the returned string. For example,

     fmt = Sprintf( "%%-%d.%ds", 16, 16 )

produces a format string result: fmt = %-16.16s

Example

The script below formats 2 values to a string and then prints the result to the Script Messages window:

 

s = "My string" ; n = 12.553

-- values to format

s = Sprintf("'%s': v=%-12lg\n", s, n/2.1 ))

-- format the string

Printf( s )

-- print the string

Related Topics

Sprintf, CFile:Printf, Printf


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.