String Addon
Contents
explode
table string.explode(string str, string delim)
Splits string 'str' by delimiter 'delim'. Returns results as a table.
trim
string string.trim(string str)
"Trim" whitespace off head and tail of string 'str', return result.
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 |
toUnicode
string string.toUnicode(string str)
Attempt to convert the input string 'str' to a wide string.
likeness
percent,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:
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":
--[[
Prints: Likeness: 81.818181 2
These two strings have two differences (the 'W' and 'H' due to case-sensitivity)
--]]
print("Likeness:", string.likeness(str1, str2));