Difference between revisions of "String Addon"
(Created page with "== explode() == '''table string.explode(string str, string delim)''' Splits string 'str' by delimiter 'delim'. Returns results as a table. == trim() == '''string string.tri...") |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | == explode | + | == explode == |
'''table string.explode(string str, string delim)''' | '''table string.explode(string str, string delim)''' | ||
| Line 5: | Line 5: | ||
| − | == trim | + | == trim == |
'''string string.trim(string str)''' | '''string string.trim(string str)''' | ||
| Line 11: | Line 11: | ||
| − | == random | + | == random == |
'''string string.random(string type, number length)''' | '''string string.random(string type, number length)''' | ||
| Line 29: | Line 29: | ||
| − | == toUnicode | + | == toUnicode == |
'''string string.toUnicode(string str)''' | '''string string.toUnicode(string str)''' | ||
Attempt to convert the input string 'str' to a wide string. | 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: | ||
| + | |||
| + | <source lang="lua"> | ||
| + | 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)); | ||
| + | </source> | ||
Latest revision as of 00:03, 25 May 2016
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));