File StringUtils.lua

Functions

string.cut (s, maxLen) Cut string to specified maximum length.
string.enclose (s, maxlevel) Enclose string by long brackets.
string.ends (String, Suffix) Test if string is ending with specified suffix.
string.findPattern (text, pattern) Return first matching substring or nil.
string.genNocasePattern (s) Generate case insensitive search pattern from string.
string.starts (String, Prefix) Test if string is starting with specified prefix.
string.trim (s) Trim string (remove all white spaces around string).
string:split (delimiter) Splits a string into a table by the given delimiter.
string:title () Capitalize first character in a string.


Functions

string.cut (s, maxLen)
Cut string to specified maximum length.
Release: post Mudlet 1.1.1 (TODO update before release)
Parameters
  • s:
  • maxLen:
Usage
  • Following call will return 'abc'.
        string.cut("abcde", 3)
     
  • You can easily pad string to certain length. Example bellow will print 'abcde ' e.g. pad/cut string to 10 characters.
        local s = "abcde"
        s = string.cut(s .. "          ", 10)   -- append 10 spaces
        echo("'" .. s .. "'")
     
string.enclose (s, maxlevel)
Enclose string by long brackets.
TODO what is purpose of this function?
Parameters
  • s:
  • maxlevel:
string.ends (String, Suffix)
Test if string is ending with specified suffix.
Parameters
  • String:
  • Suffix:
See also:
string.findPattern (text, pattern)
Return first matching substring or nil.
Release: post Mudlet 1.1.1 (TODO update before release)
Parameters
  • text:
  • pattern:
Usage
  • Following example will print: "I did find: Troll" string.
        local match = string.findPattern("Troll is here!", "Troll")
        if match then
           echo("I did find: " .. match)
        end
     
  • This example will find substring regardless of case.
        local match = string.findPattern("Troll is here!", string.genNocasePattern("troll"))
        if match then
           echo("I did find: " .. match)
        end
     
Return value:
  • nil or first matching substring
See also:
string.genNocasePattern (s)
Generate case insensitive search pattern from string.
Release: post Mudlet 1.1.1 (TODO update before release)
Parameters
  • s:
Usage:
  • Following example will generate and print "123[aA][bB][cC]" string.
        echo(string.genNocasePattern("123abc"))
     
Return value:
  • case insensitive pattern string
string.starts (String, Prefix)
Test if string is starting with specified prefix.
Parameters
  • String:
  • Prefix:
See also:
string.trim (s)
Trim string (remove all white spaces around string).
Release: post Mudlet 1.1.1 (TODO update before release)
Parameters
  • s:
Usage:
  • Example will print 'Troll is here!'.
        local str = string.trim("  Troll is here!  ")
        echo("'" .. str .. "'")
     
string:split (delimiter)
Splits a string into a table by the given delimiter.
Parameters
  • delimiter:
Usage:
  • Split string by ", " delimiter.
        names = "Alice, Bob, Peter"
        name_table = names:split(", ")
        display(name_table)
     
    Previous code will print out:
        table {
          1: 'Alice'
          2: 'Bob'
          3: 'Peter'
        }
     
Return value:
  • array with split strings
string:title ()
Capitalize first character in a string.
Usage
  • Variable testname is now Anna.
        testname = string.title("anna")
     
  • Example will set test to "Bob".
        test = "bob"
        test = string.title(test)
     

Valid XHTML 1.0!