Module pl.stringx

Python-style string library. see 3.6.1 of the Python reference.

If you want to make these available as string methods, then say stringx.import() to bring them into the standard string table.

Functions

at (self, idx) return the 'character' at the index.
center (s, w, ch) center-justify s with width w.
count (self, sub) count all instances of subtring in string.
endswith (self, s, first, last) does string end with the given substring?.
expandtabs (self, n) replace all tabs in s with n spaces.
isalnum (s) does s only contain alphanumeric characters?.
isalpha (s) does s only contain alphabetic characters?.
isdigit (s) does s only contain digits?.
islower (s) does s only contain lower case characters?.
isspace (s) does s only contain spaces?.
isupper (s) does s only contain upper case characters?.
join (self, seq) concatenate the strings using this string as a delimiter.
lfind (self, sub, i1, i2) find index of first instance of sub in s from the left.
lines (self) return an interator over all lines in a string
ljust (self, w, ch) left-justify s with width w.
lstrip (self, chrs) trim any whitespace on the left of s.
partition (self, ch) partition the string using first occurance of a delimiter
replace (s, old, new, n) replace up to n instances of old by new in the string s.
rfind (self, sub, first, last) find index of first instance of sub in s from the right.
rjust (s, w, ch) right-justify s with width w.
rpartition (self, ch) partition the string p using last occurance of a delimiter
rstrip (s, chrs) trim any whitespace on the right of s.
split (self, re) split a string into a list of strings using a pattern.
splitv (self, re) split a string using a pattern.
startswith (self, s2) does string start with the substring?.
strip (self, chrs) trim any whitespace on both left and right of s.


Functions

at (self, idx)
return the 'character' at the index.

Parameters:

  • self: the string
  • idx: an index (can be negative)

Return value:

    a substring of length 1 if successful, empty string otherwise.
center (s, w, ch)
center-justify s with width w.

Parameters:

  • s:
  • w: width of justification
  • ch: padding character, default ' '
count (self, sub)
count all instances of subtring in string.

Parameters:

  • self:
  • sub: substring
endswith (self, s, first, last)
does string end with the given substring?.

Parameters:

  • self:
  • s: a substring or a table of suffixes
  • first:
  • last:
expandtabs (self, n)
replace all tabs in s with n spaces. If not specified, n defaults to 8.

Parameters:

  • self:
  • n: number of spaces to expand each tab
isalnum (s)
does s only contain alphanumeric characters?.

Parameters:

  • s:
isalpha (s)
does s only contain alphabetic characters?.

Parameters:

  • s:
isdigit (s)
does s only contain digits?.

Parameters:

  • s:
islower (s)
does s only contain lower case characters?.

Parameters:

  • s:
isspace (s)
does s only contain spaces?.

Parameters:

  • s:
isupper (s)
does s only contain upper case characters?.

Parameters:

  • s:
join (self, seq)
concatenate the strings using this string as a delimiter.

Parameters:

  • self:
  • seq: a table of strings or numbers

Usage:

    (' '):join {1,2,3} == '1 2 3'
lfind (self, sub, i1, i2)
find index of first instance of sub in s from the left.

Parameters:

  • self:
  • sub: substring
  • i1: start index
  • i2:
lines (self)
return an interator over all lines in a string

Parameters:

  • self: the string

Return value:

    an iterator
ljust (self, w, ch)
left-justify s with width w.

Parameters:

  • self:
  • w: width of justification
  • ch: padding character, default ' '
lstrip (self, chrs)
trim any whitespace on the left of s.

Parameters:

  • self:
  • chrs:
partition (self, ch)
partition the string using first occurance of a delimiter

Parameters:

  • self:
  • ch: delimiter

Return value:

    part before ch, ch, part after ch
replace (s, old, new, n)
replace up to n instances of old by new in the string s. if n is not present, replace all instances.

Parameters:

  • s:
  • old:
  • new:
  • n:
rfind (self, sub, first, last)
find index of first instance of sub in s from the right.

Parameters:

  • self:
  • sub: substring
  • first: first index
  • last: last index
rjust (s, w, ch)
right-justify s with width w.

Parameters:

  • s:
  • w: width of justification
  • ch: padding character, default ' '
rpartition (self, ch)
partition the string p using last occurance of a delimiter

Parameters:

  • self:
  • ch: delimiter

Return value:

    part before ch, ch, part after ch
rstrip (s, chrs)
trim any whitespace on the right of s.

Parameters:

  • s:
  • chrs:
split (self, re)
split a string into a list of strings using a pattern.

Parameters:

  • self: the string
  • re: a Lua string pattern (defaults to whitespace)

Usage:

    #(('one two'):split()) == 2
splitv (self, re)
split a string using a pattern. Note that at least one value will be returned!

Parameters:

  • self: the string
  • re: a Lua string pattern (defaults to whitespace)

Usage:

    a,b = line:splitv('=')

Return value:

    the parts of the string
startswith (self, s2)
does string start with the substring?.

Parameters:

  • self:
  • s2: a string
strip (self, chrs)
trim any whitespace on both left and right of s.

Parameters:

  • self:
  • chrs:

Valid XHTML 1.0!