Garry's Mod Wiki

string

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.

Methods

vararg string.byte( string string, number startPos = 1, number endPos = startPos )
Returns the given string's characters in their numeric ASCII representation. This function will throw an error if the slice length is greater than 8000 characters.
Converts a cardinal (111) number to its ordinal/sequential variation (111th). See also STNDRD for a function that returns just the suffix.
string string.char( vararg bytes )
Takes the given numerical bytes and converts them to a string.
string string.Comma( number value, string separator = "," )
Inserts commas for every third digit of a given number.
string string.dump( function func, boolean stripDebugInfo = false )
Returns the binary bytecode of the given function. This does not work with functions created in C/C++. An error will be thrown if it is
boolean string.EndsWith( string str, string end )
Returns whether or not the second passed string matches the end of the first.
table string.Explode( string separator, string str, boolean withpattern = false )
Splits a string up wherever it finds the given separator. The function string. Split is an alias of this function, except that function doesn't support using patterns. See string. Implode for the reverse operation of this function.
number, number, string string.find( string haystack, string needle, number startPos = 1, boolean noPatterns = false )
Attempts to find the specified substring in a string. This function uses Lua Patterns by default.
string string.format( string format, vararg formatParameters )
Formats the specified values into the string given.
string string.FormattedTime( number float, string format = "nil" )
Returns the time as a formatted string or as a table if no format is given.
string string.FromColor( table color )
Creates a string from a Color variable.
string string.GetChar( string str, number index )
We advise against using this. It may be changed or removed in a future update. Use either string. sub(str, index, index) or str[index]. Returns char value from the specified index in the supplied string.
Returns extension of the file.
Returns file name and extension.
Returns the path only from a file's path.
function string.gfind( string data, string pattern )
We advise against using this. It may be changed or removed in a future update. This function is removed in Lua versions later than what GMod is currently using. Use string. gmatch instead. Returns an iterator function that is called for every complete match of the pattern, all sub matches will be passed as to the loop.
function string.gmatch( string data, string pattern )
Using Patterns, returns an iterator which will return either one value if no capture groups are defined, or any capture group matches.
string, number string.gsub( string string, string pattern, string replacement, number maxReplaces = nil )
This functions main purpose is to replace certain character sequences in a string using Patterns.
string string.Implode( string separator = "", table pieces )
We advise against using this. It may be changed or removed in a future update. You really should just use table. concat. Joins the values of a table together to form a string. This is the reverse of string. Explode and is functionally identical to table. concat, but with less features.
string string.Interpolate( string str, table lookuptable )
Interpolates a given string with the given table. This is useful for formatting localized strings.
Escapes special characters for JavaScript in a string, making the string safe for inclusion in to JavaScript strings.
string string.Left( string str, number num )
Returns everything left of supplied place of that string.
number string.len( string str )
Counts the number of characters in the string (length). This is equivalent to using the length operator (#).
string string.lower( string str )
Changes any upper-case letters in a string to lower-case letters. This function doesn't work on special non-English UTF-8 characters.
vararg string.match( string string, string pattern, number startPosition = 1 )
Finds a Pattern in a string.
string string.NiceName( string text )
Converts a "string_likeThis" to a more human-friendly "String like This". This is used internally by Faceposer and other code to transform flex and bodygroup names to a more friendly format.
string string.NiceSize( number bytes )
Converts a digital filesize to human-readable text.
string string.NiceTime( number num )
Formats the supplied number (in seconds) to the highest possible time unit.
string string.PatternSafe( string str )
Escapes all special characters within a string, making the string safe for inclusion in a Lua pattern.
string string.rep( string str, number repetitions, string separator = "" )
Repeats a string by the provided number, with an optional separator.
string string.Replace( string str, string find, string replace )
Replaces all occurrences of the supplied second string.
string string.reverse( string str )
Reverses a string.
string string.Right( string str, number num )
Returns the last n-th characters of the string.
string string.SetChar( string InputString, number Index, string ReplacementChar )
Sets the character at the specific index of the string.
table string.Split( string Inputstring, string Separator )
Splits the string into a table of strings, separated by the second argument. This is an alias of string. Explode, but with flipped arguments.
boolean string.StartsWith( string inputStr, string start )
Returns whether or not the first string starts with the second.
boolean string.StartWith( string inputStr, string start )
We advise against using this. It may be changed or removed in a future update. Use string. StartsWith. Returns whether or not the first string starts with the second. This is a alias of string. StartsWith.
Removes the extension of a path.
string string.sub( string string, number StartPos, number EndPos = nil )
Returns a sub-string, starting from the character at position StartPos of the string (inclusive), and optionally ending at the character at position EndPos of the string (also inclusive). If EndPos is not given, the rest of the string is returned.
table string.ToColor( string Inputstring )
Fetches a Color type from a string.
Returns given time in "MM:SS" format.
Returns given time in "MM:SS:MS" format.
table string.ToTable( string str )
Splits the string into characters and creates a sequential table of characters. As a result of the encoding, non-ASCII characters will be split into more than one character in the output table. Each character value in the output table will always be 1 byte.
string string.Trim( string Inputstring, string Char = "%s" )
Removes leading and trailing matches of a string.
string string.TrimLeft( string str, string char = "%s" )
Removes leading spaces/characters from a string.
string string.TrimRight( string str, string char = "%s" )
Removes trailing spaces/passed character from a string.
string string.upper( string str )
Changes any lower-case letters in a string to upper-case letters. This function doesn't work on special non-English UTF-8 characters.