The package extends default Lua's string library with several useful common methods which are present in most other languages. If you ever missed split, trim and other functions the this package provides such functions by extending Lua's default string library.
Table of contents
Via LuaRocks:
luarocks install lua-string
Or just download and require init.lua
file from this repo.
Just require it in the code like in the example below:
require "lua-string" -- It will extend the default string library
("Hello world!"):trimend("!"):sub(6):trim():totable() -- {"H", "e", "l", "l", "o"}
Overloads *
operator. Works the same as string.rep()
function.
("abc") * 2 -- "abcabc"
Overloads []
operator. It's possible to access individual chars with this operator. Index could be negative. In that case the counting will start from the end.
("abc")[1] -- "a"
("abc")[-1] -- "c"
Splits string by supplied separator. If the pattern
parameter is set to true then the separator is considered as a pattern
("a b c"):split(" ") -- {"a", "b", "c"}
("a,b, c"):split("%s*,%s*", true) -- {"a", "b", "c"}
Trims string's characters from its endings. Trims whitespaces by default. The chars
argument is a pattern string containing which characters to trim
(" abc "):trim() -- "abc"
(" abc !"):trim("! ") -- "abc"
Trims string's characters from its left side. Trims whitespaces by default. The chars
argument is a pattern string containing which characters to trim
(" abc "):trimstart() -- "abc "
(" abc !"):trimstart("! ") -- "abc !"
Trims string's characters from its right side. Trims whitespaces by default. The chars
argument is a pattern string containing which characters to trim
(" abc "):trimend() -- " abc"
(" abc !"):trimend("! ") -- " abc"
Pads string at the start with specified string until specified length. " "
pad string by default
("1"):padstart(3) -- " 1"
("1"):padstart(3, "0") -- "001"
Pads string at the end with specified string until specified length. " "
pad string by default
("1"):padend(3) -- "1 "
("1"):padend(3, "0") -- "100"
If the string starts with specified prefix then returns string itself, otherwise pads the string until it starts with the prefix
("domain.com"):ensurestart("https://") -- "https://domain.com"
("https://domain.com"):ensurestart("https://") -- "https://domain.com"
If the string ends with specified prefix then returns string itself, otherwise pads the string until it ends with the prefix
("path"):ensureend("/") -- "path/"
("path/"):ensureend("/") -- "path/"
Adds backslashes before "
, '
and \
characters. Escape character can be specified ("\\"
by default) as well as characters to escape ({"\"", "'", "\\"}
by default)
("Quote'"):esc() -- "Quote\\'"
("string%"):esc("#", {"%"}) -- "string#%"
Strips backslashes from the string. Escape character can be specified ("\\"
by default)
("Quote\\'"):unesc() -- "Quote'"
("string#%"):unesc("#") -- "string%"
Escapes pattern special characters so the can be used in pattern matching functions as is
("^[abc]"):escpattern() -- "%^%[abc%]"
Unescapes pattern special characters
("%^%[abc%]"):unescpattern() -- "^[abc]"
Escapes pattern special characters so the can be used in pattern functions as is. Deprecated, use escpattern
instead
("^[abc]"):escregex() -- "%^%[abc%]"
Unescapes pattern special characters. Deprecated, use unescpattern
instead
("%^%[abc%]"):unescregex() -- "^[abc]"
Returns an iterator which can be used in for loops
for char in ("abc"):iter() do
print(char)
end
> a
> b
> c
Truncates string to a specified length with optional suffix (usually "..."
, nil by default)
("string"):truncate(3) -- "str"
("string"):truncate(5, "...") -- "st..."
Returns true if the string starts with specified string
("string"):startswith("str") -- true
Returns true if the string ends with specified string
("string"):endswith("ing") -- true
Returns true if string's length is 0
(""):isempty() -- true
Returns true if string consists of whitespace characters
(" "):isblank() -- true
Converts "1"
, "true"
, "on"
, "yes"
, "y"
and their contraries into real boolean. Returns nil if casting cannot be done. Case-insensetive
("true"):tobool() -- true
("off"):tobool() -- false
("string"):tobool() -- nil
Returns table containing all the chars in the string
("abc"):totable() -- {"a", "b", "c"}
Install luaunit package:
luarocks install luaunit
Then run from the console:
lua test.lua