Category:string

From Garry's Mod
Jump to: navigation, search

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.

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 Meta Tables and Metamethods 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.

This category lists functions available in the string library.

Personal tools
Navigation