WoW:API format: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(Fixed slight whoops with template call.)
mNo edit summary
Line 2: Line 2:
  string.format(formatstring, e1, e2, ...)
  string.format(formatstring, e1, e2, ...)


Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q puts quotes around a string.
Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q returns string in a format that can safely be read back by Lua interpreter (puts quotes around a string and escapes special characters), it has little or no use in addon programming, but used by World of Warcraft to preparse all strings before saving them between sessions.


* c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument.
* c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument.

Revision as of 23:48, 3 July 2006

Lua/Libshortcut

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

Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q returns string in a format that can safely be read back by Lua interpreter (puts quotes around a string and escapes special characters), it has little or no use in addon programming, but used by World of Warcraft to preparse all strings before saving them between sessions.

  • c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument.
  • q and s expect a string.
> = string.format("%s %q", "Hello", "Lua user!")   -- string and quoted string
Hello "Lua user!"
> = string.format("%c%c%c", 76,117,97)             -- char
Lua
> = string.format("%e, %E", math.pi,math.pi)       -- exponent
3.141593e+000, 3.141593E+000
> = string.format("%f, %g, %.2f", math.pi, math.pi, math.pi)       -- float and compact float
3.141593, 3.14159, 3.14
> = string.format("%d, %i, %u", -100,-100,-100)    -- signed, signed, unsigned integer
-100, -100, 4294967196
> = string.format("%o, %x, %X", -100,-100,-100)    -- octal, hex, hex
37777777634, ffffff9c, FFFFFF9C

Template:LUA