String class
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
{name} placeholders in a string with the values from a hash.::-separated path for as long as it points to existing tables.::-separated path such as 'ActiveRecord::Base' points to.Methods
+ operator concatenate strings.Functions
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
Converts the first character of the string to uppercase.
Parameters
- str String
Returns
- String
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
nilsubstring to remove from both ends
Returns
- String the trimmed string
Counts how many times a character occurs in the string.
Parameters
- str String
- char String single character
Returns
- Number amount of occurrences
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
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
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
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
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
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
1position to start searching from
Returns
- Number start position of the substring or nil if it was not found
- Number end position
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
Checks whether the string is full lowercase or not.
Parameters
- str String
Returns
- Boolean
Checks whether the string can be converted to a number.
Parameters
- char String
Returns
- Boolean
Checks whether the string is full uppercase or not.
Parameters
- str String
Returns
- Boolean
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
_Gtable 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)
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
_Gtable 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)
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
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
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
falsemake the first letter lowercase instead of uppercase - no_period Boolean optional, defaults to
falsedo not append the period
Returns
- String the corrected string
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
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
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
falsekeep removing while the string still ends with needle
Returns
- String the trimmed string
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
falsekeep removing while the string still starts with needle
Returns
- String the trimmed string
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
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
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