Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string.format options

Tags:

This may seem like a stupid question, but what are the symbols used for string replacement in string.format? can someone point me to a simple example of how to use it?

like image 590
RCIX Avatar asked Nov 28 '09 07:11

RCIX


People also ask

What does %s mean in Lua?

Lua uses %s in patterns (Lua's version of regular expressions) to mean "whitespace". %s+ means "one or more whitespace characters".

What does Gmatch do in Lua?

Lua Pattern matching The `gmatch` function gmatch function will take an input string and a pattern. This pattern describes on what to actually get back. This function will return a function which is actually an iterator. The result of this iterator will match to the pattern.

What is a string value in Lua?

A StringValue is an object whose purpose is to store a single Lua string. The length of the string can't be more than 200,000 characters (this will cause a “String too long” error). Like all “-Value” objects, this single value is stored in the Value property.


2 Answers

string.format in Lua follows the same patterns as Printf in c:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

There are some exceptions, for those see here:

http://pgl.yoyo.org/luai/i/string.format

like image 81
Robert Harvey Avatar answered Oct 16 '22 17:10

Robert Harvey


Chapter 20 of PiL describes string.format near the end:

The function string.format is a powerful tool when formatting strings, typically for output. It returns a formatted version of its variable number of arguments following the description given by its first argument, the so-called format string. The format string 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 must be placed in the formatted string.

The Lua Reference says:

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 function is implemented by str_format() in strlib.c which itself interprets the format string, but defers to the C library's implementation of sprintf() to actually format each field after determining what type of value is expected (string or number, essentially) to correspond to each field.

like image 33
RBerteig Avatar answered Oct 16 '22 17:10

RBerteig