Module stringx

A module that contains many helpful string extensions.

Functions

escape_pattern (s) Escape a string so it can be used as a pattern.
isalpha (s) Checks if a string contains only alphabetic characters.
isdigit (s) Checks if a string contains only digits.
isalnum (s) Checks if a string contains only alphanumeric characters.
isspace (s) Checks if a string contains only spaces.
islower (s) Checks if a string contains only lower-case characters.
isupper (s) Checks if a string contains only upper-case characters.
at (s, i) Return the single character at the given index.
shorten (s, len, show_tail) Shortens a string to a given length, adding dots if necessary.
capitalize (s) Capitalize the first word in a string.
chomp (s, final) Strip a final value (default: newlines) from the end of a string
chop (s) Remove the final character from the string.
delete (s, substr) Remove all characters given from the string.
lstrip (s, chars) Remove leading characters from a string, as given by the chars pattern.
rstrip (s, chars) Remove trailing characters from a string, as given by the chars pattern.
strip (s, chars) Remove leading and trailing characters from a string, as given by the chars pattern.
lfind (s, sub, first, last) Finds the index of the first instance of sub in s
rfind (s, sub, first, last) Finds the index of the last instance of sub in s
replace (s, old, new, count) Replaces all instances of substring old replaced by new.
endswith (s, suffix, start, fin) Returns true if s ends with the specified suffix, false otherwise.
startswith (s, suffix, start, fin) Returns true if s starts with the specified suffix, false otherwise.
count (s, substr, start, fin) Returns the number of occurences of the substring substr in the string given by s[start:fin].
partition (s, sep) Search the string s for a pattern, and returns the part before it, the match, and the part after it.
rpartition (s, sep) Search the string s for a pattern, starting from the end, and return the part before it, the match, and the part after it.
ljust (s, i, padstr) Left-justifies the string to the given width.
rjust (s, i, padstr) Right-justifies the string to the given width.
zfill (s, width) Pad a string with zeros on the left, to fill a field of the specified width.
patch () Adds all the functions in this module to the 'string' table, so they can be used directly on strings.


Functions

escape_pattern (s)
Escape a string so it can be used as a pattern.

Parameters:

  • s The string to escape

Returns:

    A string with any special characters escaped, such that it can be used as a pattern
isalpha (s)
Checks if a string contains only alphabetic characters.

Parameters:

  • s The string to check

Returns:

    true or false
isdigit (s)
Checks if a string contains only digits.

Parameters:

  • s The string to check

Returns:

    true or false
isalnum (s)
Checks if a string contains only alphanumeric characters.

Parameters:

  • s The string to check

Returns:

    true or false
isspace (s)
Checks if a string contains only spaces.

Parameters:

  • s The string to check

Returns:

    true or false
islower (s)
Checks if a string contains only lower-case characters.

Parameters:

  • s The string to check

Returns:

    true or false
isupper (s)
Checks if a string contains only upper-case characters.

Parameters:

  • s The string to check

Returns:

    true or false
at (s, i)
Return the single character at the given index.

Parameters:

  • s The string
  • i The index

Returns:

    A string of length 1 if successful, otherwise an empty string
shorten (s, len, show_tail)
Shortens a string to a given length, adding dots if necessary.

Parameters:

  • s The string to shorten
  • len The maximum length to show
  • show_tail Boolean indicating whether to show the start or end of the string (defaults to false, indicating show the start)

Returns:

    A string of maximum length len
capitalize (s)
Capitalize the first word in a string.

Parameters:

  • s The string to capitalize

Returns:

    The string, with the first letter capitalized
chomp (s, final)
Strip a final value (default: newlines) from the end of a string

Parameters:

  • s The string to chomp
  • final The value to remove from the end of s

Returns:

    The string, with any final characters removed
chop (s)
Remove the final character from the string. If the string ends with a newline pair (\r\n), then both characters are removed.

Parameters:

  • s The string to chop

Returns:

    The string, with the final character (or newline pair) removed
delete (s, substr)
Remove all characters given from the string.

Parameters:

  • s The string to delete from
  • substr Either a list-like table of strings or single string to remove

Returns:

    The string with all input strings removed
lstrip (s, chars)
Remove leading characters from a string, as given by the chars pattern.

Parameters:

  • s The string to strip
  • chars The pattern used to look for leading characters (default: %s+)

Returns:

    A string with leading characters matching the pattern removed
rstrip (s, chars)
Remove trailing characters from a string, as given by the chars pattern.

Parameters:

  • s The string to strip
  • chars The pattern used to look for trailing characters (default: %s+)

Returns:

    A string with trailing characters matching the pattern removed
strip (s, chars)
Remove leading and trailing characters from a string, as given by the chars pattern.

Parameters:

  • s The string to strip
  • chars The pattern used to look for leading and trailing characters (default: %s+)

Returns:

    A string with leading and trailing characters matching the pattern removed
lfind (s, sub, first, last)
Finds the index of the first instance of sub in s

Parameters:

  • s The string to search
  • sub The string to search for
  • first The start index
  • last The last index

Returns:

    The index of the first instance, or nil if not found
rfind (s, sub, first, last)
Finds the index of the last instance of sub in s

Parameters:

  • s The string to search
  • sub The string to search for
  • first The start index
  • last The last index

Returns:

    The index of the last instance, or nil if not found
replace (s, old, new, count)
Replaces all instances of substring old replaced by new.

Parameters:

  • s The string to search
  • old The substring to search for
  • new The substring to replace with
  • count (optional) If given, this function will only perform this number of replacements.

Returns:

    The new string
endswith (s, suffix, start, fin)
Returns true if s ends with the specified suffix, false otherwise.

Parameters:

  • s The string to check
  • suffix A string or list-like table of suffixes to check
  • start (optional) The index to start checking at. Defaults to 1
  • fin (optional) The index to stop checking at. Defaults to #s

Returns:

    true or false
startswith (s, suffix, start, fin)
Returns true if s starts with the specified suffix, false otherwise.

Parameters:

  • s The string to check
  • suffix A string or list-like table of suffixes to check
  • start (optional) The index to start checking at. Defaults to 1
  • fin (optional) The index to stop checking at. Defaults to #s

Returns:

    true or false
count (s, substr, start, fin)
Returns the number of occurences of the substring substr in the string given by s[start:fin].

Parameters:

  • s The string to check
  • substr The substring to search for
  • start The index at which to start searching (defaults to 1)
  • fin The index at which to finish searching (defaults to #s)

Returns:

    A number indicating the number of times the substring was found
partition (s, sep)
Search the string s for a pattern, and returns the part before it, the match, and the part after it. If not found, returns the input string and two empty strings.

Parameters:

  • s The string to partition
  • sep The separator to partition by

Returns:

  1. The part before the separator, or the full string if no match
  2. The separator, or a blank string if not found
  3. The part after the separator, or a blank string if not found
rpartition (s, sep)
Search the string s for a pattern, starting from the end, and return the part before it, the match, and the part after it. If not found, returns the input string and two empty strings.

Parameters:

  • s The string to partition
  • sep The separator to partition by

Returns:

  1. The part before the separator, or the full string if no match
  2. The separator, or a blank string if not found
  3. The part after the separator, or a blank string if not found
ljust (s, i, padstr)
Left-justifies the string to the given width.

Parameters:

  • s The string to left-justify
  • i The width to justify to
  • padstr The padding string to use (defaults to ' ')

Returns:

    The newly justified string
rjust (s, i, padstr)
Right-justifies the string to the given width.

Parameters:

  • s The string to right-justify
  • i The width to justify to
  • padstr The padding string to use (defaults to ' ')

Returns:

    The newly justified string
zfill (s, width)
Pad a string with zeros on the left, to fill a field of the specified width. The string is never truncated.

Parameters:

  • s The string to pad
  • width The width to pad to
patch ()
Adds all the functions in this module to the 'string' table, so they can be used directly on strings. Note that we exclude this function itself.
generated by LDoc 1.3.12