Extensions of the string library, which can also be called as methods of any string, as in name:to_id(). Adds snake_case aliases for the built-in functions, trimming and case checks that are aware of UTF-8, conversions between naming styles (string.underscore, string.camel_case, string.to_id), lookups of tables by a ::-separated path, which class and mod are built on, {name} placeholders with string.fmt, and a + operator that concatenates strings.

Summary

Functions

Converts a snake_case string to ConstantStyle, such as 'active_record' to 'ActiveRecord'.
Converts the first character of the string to uppercase.
Removes all newlines (\n and \r) from the end of the string.
Counts how many times a character occurs in the string.
Appends the ending to the string, unless the string already ends with it.
Prepends the start to the string, unless the string already starts with it.
Escapes the control characters (\a, \b, \f, \n, \r, \t and \v) in a string, so that they appear as their backslash sequences.
Finds all occurrences of a pattern in a string.
Replaces the {name} placeholders in a string with the values from a hash.
Checks whether the string contains a substring.
Checks if the string is a command or not, i.e. whether it starts with one of the configured command prefixes.
Checks whether the string is full lowercase or not.
Checks whether the string can be converted to a number.
Checks whether the string is full uppercase or not.
Follows a ::-separated path for as long as it points to existing tables.
Finds the table that a ::-separated path such as 'ActiveRecord::Base' points to.
Returns the string itself if it is not empty.
Indents every line of the string.
Fixes the spelling of a sentence: capitalizes its first letter and appends a period, unless it already ends with '.', '!', '?' or '"'.
Splits a string into pieces around a separator.
Converts a string to an ID: lowercases it, replaces spaces with underscores and removes the characters that IDs must not have (quotes, slashes and most punctuation).
Removes a substring from the end of the string.
Removes a substring from the beginning of the string.
Converts a ConstantStyle or camelCase string to snake_case.
Checks whether the character is a vowel or not.

Methods

Makes the + operator concatenate strings.

Functions

string.camel_case(str)

shared lib/stdlib/sh_string.lua:367

Converts a snake_case string to ConstantStyle, such as 'active_record' to 'ActiveRecord'.

Parameters

  • str String

Returns

  • String the converted string
  • Number amount of underscores that were removed

string.capitalize(str)

shared lib/stdlib/sh_string.lua:389

Converts the first character of the string to uppercase.

Parameters

  • str String

Returns

  • String

string.chomp(str, what)

shared lib/stdlib/sh_string.lua:376

Removes all newlines (\n and \r) from the end of the string. If what is given, removes all occurrences of it from both the beginning and the end of the string instead.

Parameters

  • str String
  • what String optional, defaults to nil substring to remove from both ends

Returns

  • String the trimmed string

string.count(str, char)

shared lib/stdlib/sh_string.lua:306

Counts how many times a character occurs in the string.

Parameters

  • str String
  • char String single character

Returns

  • Number amount of occurrences

string.ensure_end(str, ending)

shared lib/stdlib/sh_string.lua:263

Appends the ending to the string, unless the string already ends with it.

Parameters

  • str String
  • ending String

Returns

  • String string that ends with the ending

string.ensure_start(str, start)

shared lib/stdlib/sh_string.lua:273

Prepends the start to the string, unless the string already starts with it.

Parameters

  • str String
  • start String

Returns

  • String string that starts with the start

string.escape(str)

shared lib/stdlib/sh_string.lua:499

Escapes the control characters (\a, \b, \f, \n, \r, \t and \v) in a string, so that they appear as their backslash sequences.

Parameters

  • str String

Returns

  • String the escaped string
  • Number amount of \v characters that were escaped

string.find_all(str, pattern)

shared lib/stdlib/sh_string.lua:176

Finds all occurrences of a pattern in a string.

local hits = string.find_all('{data:rank} {callback:get_name}', '{([%w_]+):([%w_]+)}')
-- hits[1] = { text = '{data:rank}', start_pos = 1, end_pos = 11, matches = { 'data', 'rank' } }

Parameters

  • str String string to search in
  • pattern String Lua pattern to search for

Returns

  • List<Map> a hash with the text, start_pos, end_pos and matches (captures) keys for every occurrence, or nil if str or pattern is missing

string.fmt(format, data)

shared lib/stdlib/sh_string.lua:487

Replaces the {name} placeholders in a string with the values from a hash. Placeholders that have no value are removed.

string.fmt('{count} {id} were registered.', { id = 'tools', count = 5 })
-- '5 tools were registered.'

Parameters

  • format String text with placeholders made of letters, digits and underscores
  • data Map optional, defaults to {} placeholder name => String/Number value

Returns

  • String the formatted string
  • Number amount of placeholders that were replaced

string.include(str, substring, start_pos)

shared lib/stdlib/sh_string.lua:209

Checks whether the string contains a substring. The substring is searched for as plain text, not as a pattern.

Parameters

  • str String string to search in
  • substring String text to search for
  • start_pos Number optional, defaults to 1 position to start searching from

Returns

  • Number start position of the substring or nil if it was not found
  • Number end position

string.is_command(str)

shared lib/stdlib/sh_string.lua:219

Checks if the string is a command or not, i.e. whether it starts with one of the configured command prefixes. If several prefixes match, the longest one is used, so that prefixes can contain each other (e.g. '/' and '//'). The StringIsCommand hook can return false to prevent that.

Parameters

  • str String

Returns

  • Boolean whether the string is a command
  • Number length of the prefix if it is one

string.is_lower(str)

shared lib/stdlib/sh_string.lua:159

Checks whether the string is full lowercase or not.

Parameters

  • str String

Returns

  • Boolean

string.is_n(char)

shared lib/stdlib/sh_string.lua:298

Checks whether the string can be converted to a number.

Parameters

  • char String

Returns

  • Boolean

string.is_upper(str)

shared lib/stdlib/sh_string.lua:152

Checks whether the string is full uppercase or not.

Parameters

  • str String

Returns

  • Boolean

string.parse_parent(str, ref)

shared lib/stdlib/sh_string.lua:427

Follows a ::-separated path for as long as it points to existing tables. Returns the last table it reached and the name to look up or create in it.

-- While Flux exists and Flux.Thing does not:
local parent, name = ('Flux::Thing'):parse_parent() -- Flux, 'Thing'
local parent, name = ('Thing'):parse_parent()       -- _G, 'Thing'

Parameters

  • str String path to the table
  • ref Map optional, defaults to _G table to start the lookup from

Returns

  • Map the last existing table on the path
  • String the first part of the path that is not a table (the last part if the whole path already exists)

string.parse_table(str, ref)

shared lib/stdlib/sh_string.lua:402

Finds the table that a ::-separated path such as 'ActiveRecord::Base' points to.

local base_class = ('ActiveRecord::Base'):parse_table() -- same as _G.ActiveRecord.Base

Parameters

  • str String path to the table
  • ref Map optional, defaults to _G table to start the lookup from

Returns

  • Map/Boolean the table or false if it was not found
  • String the part of the path that is not a table (only on failure)

string.presence(str)

shared lib/stdlib/sh_string.lua:345

Returns the string itself if it is not empty.

Parameters

  • str Any value to check

Returns

  • String the same string, or nil if it is empty or not a string at all

string.set_indent(str, indent)

shared lib/stdlib/sh_string.lua:283

Indents every line of the string. Lines that would only consist of whitespace are left empty.

Parameters

  • str String
  • indent String text to put in front of every line, such as a few spaces

Returns

  • String the indented string

string.spelling(str, first_lower, no_period)

shared lib/stdlib/sh_string.lua:324

Fixes the spelling of a sentence: capitalizes its first letter and appends a period, unless it already ends with '.', '!', '?' or '"'. Strings in full uppercase keep their letter case.

Parameters

  • str String
  • first_lower Boolean optional, defaults to false make the first letter lowercase instead of uppercase
  • no_period Boolean optional, defaults to false do not append the period

Returns

  • String the corrected string

string.split(str, sep)

shared lib/stdlib/sh_string.lua:62

Splits a string into pieces around a separator. Splits the string into individual characters if no separator is given.

Parameters

  • str String string to split
  • sep String optional, defaults to '' separator

Returns

  • List<String> pieces

string.to_id(str)

shared lib/stdlib/sh_string.lua:250

Converts a string to an ID: lowercases it, replaces spaces with underscores and removes the characters that IDs must not have (quotes, slashes and most punctuation).

Parameters

  • str String

Returns

  • String the ID

string.trim_end(str, needle, all_occurrences)

shared lib/stdlib/sh_string.lua:104

Removes a substring from the end of the string.

Parameters

  • str String the string to trim
  • needle String substring to remove
  • all_occurrences Boolean optional, defaults to false keep removing while the string still ends with needle

Returns

  • String the trimmed string

string.trim_start(str, needle, all_occurrences)

shared lib/stdlib/sh_string.lua:129

Removes a substring from the beginning of the string.

Parameters

  • str String the string to trim
  • needle String substring to remove
  • all_occurrences Boolean optional, defaults to false keep removing while the string still starts with needle

Returns

  • String the trimmed string

string.underscore(str)

shared lib/stdlib/sh_string.lua:356

Converts a ConstantStyle or camelCase string to snake_case. Namespace separators (::) become slashes, dashes and whitespace become underscores.

('ActiveRecord::Base'):underscore() -- 'active_record/base'

Parameters

  • str String

Returns

  • String the snake_case string

string.vowel(char)

shared lib/stdlib/sh_string.lua:80

Checks whether the character is a vowel or not. On the client the current language can override the result with its is_vowel function.

Parameters

  • char String single character

Returns

  • Boolean true if it is a vowel, nil (or the language's own answer) otherwise

Methods

String:__add(right)

shared lib/stdlib/sh_string.lua:291

Makes the + operator concatenate strings. Strings that can be converted to numbers are still added up as numbers by Lua itself.

Parameters

  • right Any right-hand operand, converted with tostring

Returns

  • String the concatenated string

Defined in