Formatting and Converting Strings
Formatting and Converting Strings
String formatting is used, well, to format strings. This is done through the
string.format() function; something that requires specific instructions. These instructions follow the format
string.format() accepts unlimited arguments.
The most important part of string formatting is the specifiers:
|d or i||integer||Decimal representation||321|
|e/E||float||Scientific notation using lowercase e/E||3.296e2/3.296E2|
|g/G||float||The shorter of e/E and f||3E14/3e14|
|q||string||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||"print(\"Hi\")"|
|u||positive integer||Decimal representation||3131|
|%||A % followed by another % will return the % sign itself||%|
print( ("The magic word is %s"):format("roblox") )
The magic word is roblox
print( ("The magic word is %q"):format("roblox") )
The magic word is "roblox"
print( ("The scientific notation of %d is %E"):format(3901, 3901) )
The scientific notation of 3901 is 3.901000E+03
print( ("It is shorter to use scientific notation for %G"):format(3000000) )
It is shorter to use scientific notation for 3E+06
print( ("%q"):format(**Ahaha "quotes", a \n new line, and \0 an embedded zero!**) )
"Ahaha \"quotes\", a \ new line, and \0 an embedded zero!"
print( ("Look! A string of hexadecimal characters --> %x"):format(5002) )
Look! A string of hexadecimal characters --> 3a9a
Flags and Width
|-||Left-justify the given field width. Right justification is the default (see "width" below).|
|+||Forces to precede by a + or - sign, even if the number is positive. The default only shows the - sign in front of a negative number.|
|(space)||A blank space is inserted before the value.|
|#||Used with o and x/X, writes a 0 (octal) or 0x/0X (hex) before the values for values other than zero.
Used with e/E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
|0||Left-pads the number with 0s instead of empty spaces (see "width" below).|
|(number)||Minimum number of characters to return. If the number of characters to be formatted is less than this number, then the result is padded with blank spaces. Otherwise, nothing happens.|
300 -- There are 7 spaces there. I counted. :D
print(string.format("%+i is less than %i", 300, 3000)) -- First %i for the second argument, second %i for the third
+300 is less than 3000
print(string.format("You can declare a hexadecimal number by starting it with 0x, like this: %#x.", 1))
You can declare a hexadecimal number by starting it with 0x, like this: 0x1.
print(string.format("There is a% i%% chance of rain in Seattle, today.", 300))
There is a 300% chance of rain in Seattle, today.
|.(number)||For integer specifiers (d, i, o, u, x/X): precision specifies the minimum number of digits to be returned. If the value to be formatted is shorter than this number, the result is padded with leading zeros. A precision of 0 means that no character is written for the value 0.
For e/E and f specifiers: this is the number of digits to be printed after the decimal point.
For g/G specifiers: This is the maximum number of digits (before the e/E, if present).
For s: this is the maximum number of characters to be returned.
For c and q types: it has no effect.
When no precision is specified, the default is 1. If the period is given without a value for precision, 0 is assumed.
-- Pad it with 0s to make sure there are 12 digits, put 3 numbers before the E, and return it in scientific notation print(string.format("%012.3E", 301141))
-- Return first 3 letters of the string print(string.format("string.format > string.sub. %.3s", "Harry Potter trolls!"))
string.format > string.sub. Har
print(string.format("Once upon a time, there was a magic pony named %s, it had %i horns, and it's location in RAM was %#.9x.", "dap300", 0, 10000000000))
Once upon a time, there was a magic pony named dap300, it had 0 horns, and it's location in RAM was 0x2540be400.