String Module
#string.explode table string.explode(string str, string delim)

Splits string 'str' by delimiter 'delim'. Returns results as a table.

If 'str' ends with the delimiter, it will not include that empty/blank value.

Example:
local data = "abc,def,ghi,"; local results = string.explode(data, ","); -- Split 'data' by commas table.print(results);
Example output:
1: "abc" 2: "def" 3: "ghi"
#string.implode string string.implode(table elements, string glue)

The exact opposite of string.explode(); this function takes a table of elements and joins them by a glue string.

Example:
local items = {1, 2, 3, 4} print(string.implode(items, ",")) -- Prints: 1,2,3,4
#string.trim string string.trim(string str)

"Trim" whitespace off head and tail of string 'str', return result.

Example:
local data = " \tTrim excess white space from this string \t "; print(string.trim(data));
Example output:
Trim excess white space from this string
#string.random string string.random(string type, number length)

Creates a random string based on the given type and with 'length' characters. 'type' should be:

"alnum"Alpha-numeric (both upper and lower case) characters
"letters"Letters only (both upper and lower case)
"numbers"Numeric characters only
Example:
print( string.random("alnum", 16) ); -- Print out a random string of 16 alpha-numeric characters
Example output:
cv1H5u5Bim0mWq8r
#string.toUnicode string string.toUnicode(string str)

Attempt to convert the input string 'str' to a wide string, returns the result.

#string.likeness numberpercent, number differences string.likeness(string str1, string str2)

Computes how alike two strings are and returns two numbers: 'percent' (a float) of how similar they are and 'differences' (an integer) which is the number of differences found between them. This function is case-sensitive; if you would like to have it be case insensitive, consider casting both strings to lower-case before passing them to this function.

This function uses the Levenshtein distance algorithm. An important thing to note is that it is not a direct column-to-column comparison, but will instead consider string shifting where available. For example:

Example:
local str1 = "HelloWorld"; local str2 = "Hello World": print("Likeness:", string.likeness(str1, str2)); --[[ Prints: Likeness: 90.909090 1 These two should have only one difference: the space between them! They are ~91% similar NOT 6 differences (everything after 'Hello'). --]] local str1 = "Hello World"; local str2 = "hello world": print("Likeness:", string.likeness(str1, str2)); --[[ Prints: Likeness: 81.818181 2 These two strings have two differences (the 'W' and 'H' due to case-sensitivity) --]]

Page last updated at 2021-11-07 23:19:13


Copyright 2024