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

  1. str string The string to check.
  2. prefix string The prefix the string may start with.

Returns

  1. boolean If this string starts with the prefix.
ends_with(str, suffix)Source

Determine if a string ends with the suffix.

Parameters

  1. str string The string to check.
  2. suffix string The suffix the string may end with.

Returns

  1. 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

  1. str string The string to split.
  2. deliminator string The pattern to split this string on.
  3. plain? boolean Treat the deliminator as a plain string, rather than a pattern.
  4. 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" }
escape_pattern(pattern)Source

Escape a string for using in a pattern

Parameters

  1. pattern string The string to escape

Returns

  1. 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"