string/format

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m
(local var)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=format
 
|Parent=string
 
 
|Description=Formats the specified values into the string given.
 
|Description=Formats the specified values into the string given.
|Realm=Shared
+
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No
 
}}
 
}}
Line 9: Line 7:
 
|type=string
 
|type=string
 
|name=format
 
|name=format
|desc=The string to be formatted.
+
|desc=The string to be formatted.<br>
 +
Follows this format: http://www.cplusplus.com/reference/cstdio/printf/
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 18: Line 17:
 
{{Ret
 
{{Ret
 
|type=string
 
|type=string
|desc=formattedString
+
|desc=The formatted string
 +
}}
 +
{{Example
 +
|Description=Example showing the different types of format codes.
 +
|Code=local s = "Hello, world!"
 +
 +
// string
 +
print(string.format("here's a string: %s", s))
 +
 +
// string with quotes
 +
print(string.format("here's a quoted string: %q", s))
 +
 +
// characters from numeric values
 +
print(string.format("%c%c%c", 65, 66, 67))
 +
 +
// number with an exponent
 +
print(string.format("%e, %E", math.pi, math.pi))
 +
 +
// float and compact float
 +
print(string.format("%f, %G", math.pi, math.pi))
 +
 +
// signed, signed, and unsigned int
 +
print(string.format("%d, %i, %u", -100, -100, -100))
 +
 +
// octal, hex, and uppercase hex
 +
print(string.format("%o, %x, %X", -100, -100, -100))
 +
|OutputFixedWidth=Fixed width
 +
|Output=here's a string: Hello, world!
 +
here's a quoted string: "Hello, world!"
 +
ABC
 +
3.141593e+000, 3.141593E+000
 +
3.141593, 3.14159
 +
-100, -100, 4294967196
 +
37777777634, ffffff9c, FFFFFF9C
 
}}
 
}}

Latest revision as of 19:36, 11 July 2019

 string.format( )

Contents

Description

Formats the specified values into the string given.

Arguments

string format

The string to be formatted.

Follows this format: http://www.cplusplus.com/reference/cstdio/printf/

Arguments

vararg formatParameters

Values to be formatted into the string.

Returns

string

The formatted string

Examples

Example

Example showing the different types of format codes.

local s = "Hello, world!"
 
// string
print(string.format("here's a string: %s", s))
 
// string with quotes
print(string.format("here's a quoted string: %q", s))
 
// characters from numeric values
print(string.format("%c%c%c", 65, 66, 67))
 
// number with an exponent
print(string.format("%e, %E", math.pi, math.pi))
 
// float and compact float
print(string.format("%f, %G", math.pi, math.pi))
 
// signed, signed, and unsigned int
print(string.format("%d, %i, %u", -100, -100, -100))
 
// octal, hex, and uppercase hex
print(string.format("%o, %x, %X", -100, -100, -100))

Output:

here's a string: Hello, world!
here's a quoted string: "Hello, world!"
ABC
3.141593e+000, 3.141593E+000
3.141593, 3.14159
-100, -100, 4294967196
37777777634, ffffff9c, FFFFFF9C
Personal tools
Navigation