String Extensions
Ratchet expands on the standard string library to give you a few more functions for convenience. All standard Lua functions are available as well and not listed on this page.
Just like with standard functions, you can use semicolon to call any of these functions on an existing string and use it as the input.
explode
function
Splits a string up wherever it finds the given separator.
Syntax:
table<string> string.explode( string input, string delimiter )
Example:
local hello = "Hello world!"
print(hello:explode(" ")[1]) -- "Hello"
split
function
Split is just an alias for explode and works exactly the same.
Syntax:
table<string> string.split( string input, string delimiter )
startsWith
function
Checks whether a string starts with the specified prefix or not.
Syntax:
bool string.startsWith( string input, string prefix )
endsWith
function
Checks whether a string ends with the specified suffix or not.
Syntax:
bool string.endsWith( string input, string suffix )
trimLeft
function
Removes all whitespace characters from the left side of a string.
Syntax:
string string.trimLeft( string input )
trimRight
function
Removes all whitespace characters from the right side of a string.
Syntax:
string string.trimRight( string input )
trim
function
Removes all whitespace characters surrounding a string.
Syntax:
string string.trim( string input )
join
function
Given a table of strings, it will glue it together with a specified separator. This is essentially the same thing as table.concat
and also the opposite of explode and split.
Syntax:
string string.join( table<string> parts, string delimiter )
padLeft
function
Adds a padding character specified amount of times to the left side of a string.
Syntax:
string string.padLeft( string input, int length, string pad )
padRight
function
Adds a padding character specified amount of times to the right side of a string.
Syntax:
string string.padRight( string input, int length, string pad )
isEmpty
function
Checks whether the string contains nothing or only contains whitespace characters.
Syntax:
bool string.isEmpty( string input )
isAlpha
function
Checks whether the string only contains letters of the English alphabet.
Syntax:
bool string.isAlpha( string input )
isNumeric
function
Checks whether the string only contains numbers.
Syntax:
bool string.isNumeric( string input )
isAlphaNumeric
function
Checks whether the string only contains letters and numbers.
Syntax:
bool string.isAlphaNumeric( string input )
count
function
Searches for a substring in the input string and counts its occurances.
Syntax:
int string.count( string input, string substring )
capitalize
function
Makes first letter of every word uppercase while making everything else lowercase.
Syntax:
string string.capitalize( string input )
levenshtein
function
Calculates the Levenshtein distance of two strings. This is useful to find similar strings.
Syntax:
string string.levenshtein( string first, string second )
removePrefix
function
Removes a prefix from the beginning of the input string as long as it starts with that prefix.
Syntax:
string string.removePrefix( string input, string prefix )
removeSuffix
function
Removes a suffix from the end of the input string as long as it ends with that suffix.
Syntax:
string string.removeSuffix( string input, string suffix )
slugify
function
Converts a string into a slug (text that can be safely used in URL or as a file name).
Syntax:
string string.slugify( string input )
Example:
local slug = string.slugify("Hello world!")
print(slug) -- "hello-world"
random
function
Generates a random string of specified length. You can also specify characters it should use. By default, all numbers and all lowercase and uppercase letters of English alphabet are used.
Syntax:
string string.random( int length [, string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ] )
WARNING
This random generator DOES NOT generate cryptographically secure values, DO NOT use it for sensitive cryptographic purposes, or purposes that require returned values to be unguessable.