Difference between revisions of "String.format"

From GiderosMobile
(added example + formatting)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
 
 
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]<br/>
 
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]<br/>
=== <translate>Description</translate> ===
 
<translate>Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q. 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, newlines, embedded zeros, and backslashes in the string are correctly escaped when written. For instance, the call
 
  
    ''string.format('%q', 'a string with "quotes" and \n new line')''
+
=== Description ===
 +
Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string).
 +
 
 +
The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q.
 +
 
 +
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, newlines, embedded zeros, and backslashes in the string are correctly escaped when written. For instance, the call
 +
 
 +
''string.format('%q', 'a string with "quotes" and \n new line')''
  
will produce the string:  
+
will produce the string:  
  
    `"a string with \"quotes\" and \
+
"a string with \"quotes\" and \
      new line"`
+
new line"`
  
 +
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 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.  
+
This function does not accept string values containing embedded zeros, except as arguments to the q option.
 +
<source lang="lua">
 +
string.format(formatstring,e1,e2,...)
 +
</source>
  
 +
=== Parameters ===
 +
'''formatstring''': (string) the string defining the format of the output<br/>
 +
'''e1''': (string) first parameter for the format string '''optional'''<br/>
 +
'''e2''': (string) second parameter to format string '''optional'''<br/>
 +
'''...''': (multiple) more optional parameters for format string '''optional'''<br/>
  
This function does not accept string values containing embedded zeros, except as arguments to the q option.</translate>
+
=== Examples ===
 
<source lang="lua">
 
<source lang="lua">
string.format(formatstring,e1,e2,...)
+
local mycolor = 0xffffff
 +
local mycolorstring = ("0x%06x"):format(mycolor) -- "0x0000ff"
 +
local mycolorstring2 = ("%x"):format(mycolor) -- "ff"
 
</source>
 
</source>
=== <translate>Parameters</translate> ===
 
'''formatstring''': (string) <translate>the string defining the format of the output</translate> <br/>
 
'''e1''': (string) <translate>first parameter for the format string</translate> '''optional'''<br/>
 
'''e2''': (string) <translate>second parameter to format string</translate> '''optional'''<br/>
 
'''...''': (multiple) <translate>more optional parameters for format string</translate> '''optional'''<br/>
 
  
 
{{String}}
 
{{String}}

Revision as of 08:30, 1 September 2020


Available since: Gideros 2011.6
Class: string

Description

Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string).

The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q.

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, newlines, embedded zeros, and backslashes in the string are correctly escaped when written. For instance, the call

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

will produce the string:

"a string with \"quotes\" and \
new line"`

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.

This function does not accept string values containing embedded zeros, except as arguments to the q option.

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

Parameters

formatstring: (string) the string defining the format of the output
e1: (string) first parameter for the format string optional
e2: (string) second parameter to format string optional
...: (multiple) more optional parameters for format string optional

Examples

local mycolor = 0xffffff
local mycolorstring = ("0x%06x"):format(mycolor) -- "0x0000ff"
local mycolorstring2 = ("%x"):format(mycolor) -- "ff"