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
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!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
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.
Inserts commas for every third digit of a given number.
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
Returns whether or not the second passed string matches the end of the first.
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.
Formats the specified values into the string given.
Returns the time as a formatted string or as a table if no format is given.
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.
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.
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.
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.
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.
Returns everything left of supplied place of that string.
Counts the number of characters in the string (length). This is equivalent to using the length operator (#).
Changes any upper-case letters in a string to lower-case letters.
This function doesn't work on special non-English UTF-8 characters.
Finds a Pattern in a string.
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.
Formats the supplied number (in seconds) to the highest possible time unit.
Escapes all special characters within a string, making the string safe for inclusion in a Lua pattern.
Repeats a string by the provided number, with an optional separator.
Replaces all occurrences of the supplied second string.
Sets the character at the specific index of the string.
Splits the string into a table of strings, separated by the second argument.
This is an alias of string. Explode, but with flipped arguments.
Returns whether or not the first string starts with the second.
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.
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.
Returns given time in "MM:SS" format.
Returns given time in "MM:SS:MS" format.
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.
Removes leading and trailing matches of a string.
Removes leading spaces/characters from a string.
Removes trailing spaces/passed character from a string.