Garry's Mod Wiki


The string type is a sequence of characters.

The string library is a standard Lua library which provides functions for the manipulation of strings.[1]

In Garry's Mod there are several extra useful functions and features added to this library.
Most notably all strings will access this library through the string metatable index function.[2]

This means all strings are treated like table objects and the string library as its Meta Table

local x = "Kittens" function string.Foobar(self) return self:Right(4) end string.Right(x,4) == x:Right(4) == x:Foobar() == ("Kittens"):Right(4) == x[-4]..x[-3]..x[-2]..x[-1]

The string metatable however is something else, and to access that you must use getmetatable("").

The difference here is related to Metamethods, such as using (+) instead of (..) to concatenate strings.

Using + operator for concatenation breaks lua metamethod to sum string as numbers (Example: "10" + "1" return 11 (number)) and this is 400 times SLOWER!
local stringmeta = getmetatable("") function stringmeta.__add(str,x) return str..x end -- This will work function string.__add(str,x) return str..x end -- But this will not. print("Kittens" + " And " + "Puppies")

See Meta Tables and Metamethods for more information.

Making changes to the string metatable is not a good idea unless you know what you are doing. Use the string library instead.

This category lists functions available in the string library.

Page Links

Special Pages



Render Time: 40ms

DB GetPage 5
Generate Html 12
SaveChanges (1) 8
Render Body 0
Render Sidebar 13