Eyeon:Script/Reference/Libraries/string/format

From VFXPedia

< Eyeon:Script | Reference | Libraries | string
Revision as of 14:01, 9 December 2006 by Peter (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

String : string.format

Arguments

string.format(formatstring, e1, e2, ... )

  • formatstring (required, string)

The method used to format the provided strings

  • e1, e2, etc (required, string)

The strings to be formatted into the result

Returns

A single string containing formatted versions of e1, e2, etc.,.

Remarks

Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The formatstring argument follows the same rules as the printf family of standard C functions (see below for a description of those options). The only differences are that the options/modifiers *, l, L, n, p, and h are not supported, and there is an extra option, q.

The formatstring argument has rules similar to those of the printf function of standard C; It is composed of regular text and directives, which control where and how each argument is to be placed in the formatted string. A simple directive is the symbol % followed by a single letter that tells the function how to format the argument; d for a decimal number, h for hexadecimal, o for octal, and f for floating point. Between the % and this letter, a directive can have other options, which control the details of the format.

The q option formats a string in a form suitable to be safely read back by the Lua interpreter: The string is written between double quotes, and all double quotes, returns, and backslashes in the string are correctly escaped when written. For instance, the call

The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument, whereas q and s expect a string. The * modifier can be simulated by building the appropriate format string. For example, "%*g" can be simulated with "%"..width.."g".

Neither the formatstring nor the string values to be formatted with %s can contain embedded zeros. %q handles string values with embedded zeros.

Format String Specification

  • %i

signed integer notation.

  • %d

signed decimal notation.

  • %u

unsigned decimal notation.

  • %o

unsigned octal notation, without leading 0.

  • %x

unsigned hexadecimal, using abcdef, without leading 0x.

  • %X

unsigned hexadecimal, using ABCDEF, without leading 0X.

  • %c

single character.

  • %s

character string, not surrounded by quotes.

  • %q

character string, surrounded by quotes and formatted to be safe for the Lua interpreter, including escapes for special characters like

  • %f

double-precision floating-point.

  • %e

double-precision floating-point, exponential notation using e.

  • %E

double-precision floating-point, exponential notation using E.

  • %g

double-precision floating-point, using %f or %e.

  • %G

double-precision floating-point, using %f or %E.

  • %%

literal% (not a conversion: consumes no arguments).

Requirements

  • eyeonScript 5.0
  • Fusion 5.0

Examples

string.format('%q', 'a string with "quotes" and \n new line') -- "a string with \"quotes\" and \ new line"

d = 5; m = 11; y = 1990

string.format("%02d/%02d/%04d" , d, m, y) --"05/11/1990"

tag = "h1"; title = "A Title"

string.format("%s", tag, title, tag) -- " A Title"

num = 1024.15

string.format("%d", num) -- 1024

string.format("%06d", num) -- "001024"

string.format("%6d", num) -- "1024"

string.format("%.4f", num) -- "1024.1500"

string.format("%.1f", num) -- "1024.2"


Tips for format (edit)

EyeonTips:Script/Reference/Libraries/string/format