Garry's Mod Wiki

Revision Difference

string#511972

The string `type` is a sequence of characters.⤶ ⤶ The string `library` is a standard Lua library which provides functions for the manipulation of strings.[https://www.lua.org/pil/20.html]⤶ ⤶ In Garry's Mod there are several extra useful functions and features added to this library.<br/>⤶ Most notably all strings will access this library through the string metatable index function.[https://github.com/Facepunch/garrysmod/blob/master/garrysmod/lua/includes/extensions/string.lua#L288-L299]⤶ ⤶ This means all strings are treated like table objects and the string library as its <page text="Meta Table">Meta_Tables</page>⤶ ⤶ ```⤶ 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(""). ⤶ <br/>The difference here is related to <page>Metamethods</page>, such as using (+) instead of (..) to concatenate strings.⤶ ⤶ ```⤶ print("Kittens" + " And " + "Puppies")⤶ ⤶ function getmetatable("").__add(str,x) return str..x end // This will work⤶ function string.__add(str,x) return str..x end // But this will not.⤶ ```⤶ ⤶ See <page>Meta Tables</page> and <page>Metamethods</page> for more information.⤶ <warning>Making changes to the string `metatable` is not a good idea unless you know what you are doing. Use the string `library` instead.</warning>⤶ ⤶ This category lists functions available in the string `library`.