Extend string
Extensions to the string module.
Credit to rxi's lume lib for some of these functions.
Functions
| split (str, sep) | Split a string using a given seperator. |
| trim (str[, chars]) | Trims the whitespace from the start and end of the string. |
| simpleFormat (str, ...) | Returns a formatted string. |
| fill (str, size, pos[, ch="0"]) | Append or prepend characters to fill up a string to a specified length. |
Functions
- split (str, sep)
-
Split a string using a given seperator.
If seperator is not provided, it will split the string into seperate words.
Consecutive delimiters are not grouped together and will delimit empty strings.
Parameters:
Returns:
-
table
table of strings
Usage:
string.split("One two three") -- Returns {"One", "two", "three"} string.split("a,b,,c", ",") -- Returns {"a", "b", "", "c"}
- trim (str[, chars])
-
Trims the whitespace from the start and end of the string.
If specific string is provided, the characters in that string are trimmed from the string instead of whitespace.
Parameters:
Returns:
-
string
trimmed string
- simpleFormat (str, ...)
-
Returns a formatted string.
Alternative to Lua's string.format. The values of the keys in the provided table can be inserted into the string
using the form '{key}' in the input string. Numerical keys can also be used.
Parameters:
Usage:
string.simpleFormat("{b} hi {a}", {a = "mark", b = "Oh"}) -- Returns "Oh hi mark" string.simpleFormat("Hello {1}!", "world") -- Returns "Hello world!"
- fill (str, size, pos[, ch="0"])
-
Append or prepend characters to fill up a string to a specified length.
Parameters:
- str string input string
- size number desired length of resulting string
- pos string either "before" or "after"
- ch string character to append or prepend (default "0")
Returns:
-
string
resulting string
Usage:
string.fill("12", 5, "before", "0") -- Returns "00012" string.fill("BLAM", 10, "after", "O") -- Returns "BLAMOOOOOO"