metis.string
Extends the default string
library with additional utility functions.
This module re-exports any function defined in string
, and so may shadow the
string
global. This does not extend the string metatable though, so you
cannot use colon notation (e.g. ("hello"):sub(...)
).
local string = require "metis.string" print(string.sub("hello", 1, 1))
starts_with(str, prefix) | Determine if a string starts with the prefix. |
---|---|
ends_with(str, suffix) | Determine if a string ends with the suffix. |
split(str, deliminator [, plain [, limit]]) | Split a string according to a deliminator. |
escape_pattern(pattern) | Escape a string for using in a pattern |
- starts_with(str, prefix)Source
Determine if a string starts with the prefix.
Parameters
Returns
- boolean If this string starts with the prefix.
- ends_with(str, suffix)Source
Determine if a string ends with the suffix.
Parameters
Returns
- boolean If this string ends with the suffix.
- split(str, deliminator [, plain [, limit]])Source
Split a string according to a deliminator.
Note, the deliminator is a Lua pattern
Parameters
- str
string
The string to split. - deliminator
string
The pattern to split this string on. - plain?
boolean
Treat the deliminator as a plain string, rather than a pattern. - limit?
number
The maximum number of elements in the returned list.
Usage
Split a string into words.
local string = require "metis.string" print(textutils.serialize(string.split("This is a sentence.", "%s+"))) -- => { "This", "is", "a" , "sentence." }
Split a string by "-" into at most elements.
local string = require "metis.string" print(textutils.serialize(string.split("a-separated-string-of-sorts", "-", true, 3))) -- => { "a", "separated", "string-of-sorts" }
- str
- escape_pattern(pattern)Source
Escape a string for using in a pattern
Parameters
- pattern
string
The string to escape
Returns
string
The escaped pattern.
Usage
Escape the string "1.0 You Are (Not) Alone"
local string = require "metis.string" print(string.escape_pattern("1.0 You Are (Not) Alone")) -- => "1%.0 You are %(Not%) Alone"
- pattern