Module pl.stringx
Python-style extended 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.
See the Guide
Dependencies: pl.utils
Functions
isalpha (s) | does s only contain alphabetic characters?. |
isdigit (s) | does s only contain digits?. |
isalnum (s) | does s only contain alphanumeric characters?. |
isspace (s) | does s only contain spaces?. |
islower (s) | does s only contain lower case characters?. |
isupper (s) | does s only contain upper case characters?. |
join (self, seq) | concatenate the strings using this string as a delimiter. |
startswith (self, s2) | does string start with the substring?. |
endswith (s, send) | does string end with the given substring?. |
expandtabs (self, n) | replace all tabs in s with n spaces. |
lfind (self, sub, i1) | find index of first instance of sub in s from the left. |
rfind (self, sub, first, last) | find index of first instance of sub in s from the right. |
replace (s, old, new, n) | replace up to n instances of old by new in the string s. |
split (self, re, n) | split a string into a list of strings using a delimiter. |
splitv (self, re) | split a string using a pattern. |
count (self, sub) | count all instances of substring in string. |
ljust (self, w, ch) | left-justify s with width w. |
rjust (s, w, ch) | right-justify s with width w. |
center (s, w, ch) | center-justify s with width w. |
lstrip (self, chrs) | trim any whitespace on the left of s. |
rstrip (s, chrs) | trim any whitespace on the right of s. |
strip (self, chrs) | trim any whitespace on both left and right of s. |
partition (self, ch) | partition the string using first occurance of a delimiter |
rpartition (self, ch) | partition the string p using last occurance of a delimiter |
at (self, idx) | return the ‘character’ at the index. |
lines (self) | return an interator over all lines in a string |
title (self) | iniital word letters uppercase (‘title case’). |
shorten (self, sz, tail) | return a shorted version of a string. |
Functions
- isalpha (s)
-
does s only contain alphabetic characters?.
Parameters:
s
: a string
- isdigit (s)
-
does s only contain digits?.
Parameters:
s
: a string
- isalnum (s)
-
does s only contain alphanumeric characters?.
Parameters:
s
: a string
- isspace (s)
-
does s only contain spaces?.
Parameters:
s
: a string
- islower (s)
-
does s only contain lower case characters?.
Parameters:
s
: a string
- isupper (s)
-
does s only contain upper case characters?.
Parameters:
s
: a string
- join (self, seq)
-
concatenate the strings using this string as a delimiter.
Parameters:
self
: the stringseq
: a table of strings or numbers
Usage:
(' '):join {1,2,3} == '1 2 3'
- startswith (self, s2)
-
does string start with the substring?.
Parameters:
self
: the strings2
: a string
- endswith (s, send)
-
does string end with the given substring?.
Parameters:
s
: a stringsend
: a substring or a table of suffixes
- expandtabs (self, n)
-
replace all tabs in s with n spaces. If not specified, n defaults to 8.
with 0.9.5 this now correctly expands to the next tab stop (if you really
want to just replace tabs, use :gsub(‘\t’,‘ ’) etc)
Parameters:
self
: the stringn
: number of spaces to expand each tab, (default 8)
- lfind (self, sub, i1)
-
find index of first instance of sub in s from the left.
Parameters:
self
: the stringsub
: substringi1
: start index
- rfind (self, sub, first, last)
-
find index of first instance of sub in s from the right.
Parameters:
self
: the stringsub
: substringfirst
: first indexlast
: last index
- 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
: the stringold
: the target substringnew
: the substitutionn
: optional maximum number of substitutions
Returns:
- result string
- the number of substitutions
- split (self, re, n)
-
split a string into a list of strings using a delimiter.
Parameters:
self
: the stringre
: a delimiter (defaults to whitespace)n
: maximum number of results
Usage:
#(('one two'):split()) == 2
('one,two,three'):split(',') == List{'one','two','three'}
('one,two,three'):split(',',2) == List{'one','two,three'}
- splitv (self, re)
-
split a string using a pattern. Note that at least one value will be returned!
Parameters:
self
: the stringre
: a Lua string pattern (defaults to whitespace)
Usage:
a,b = line:splitv('=')
Returns:
-
the parts of the string
- count (self, sub)
-
count all instances of substring in string.
Parameters:
self
: the stringsub
: substring
- ljust (self, w, ch)
-
left-justify s with width w.
Parameters:
self
: the stringw
: width of justificationch
: padding character, default ‘ ’
- rjust (s, w, ch)
-
right-justify s with width w.
Parameters:
s
: the stringw
: width of justificationch
: padding character, default ‘ ’
- center (s, w, ch)
-
center-justify s with width w.
Parameters:
s
: the stringw
: width of justificationch
: padding character, default ‘ ’
- lstrip (self, chrs)
-
trim any whitespace on the left of s.
Parameters:
self
: the stringchrs
: default space, can be a string of characters to be trimmed
- rstrip (s, chrs)
-
trim any whitespace on the right of s.
Parameters:
s
: the stringchrs
: default space, can be a string of characters to be trimmed
- strip (self, chrs)
-
trim any whitespace on both left and right of s.
Parameters:
self
: the stringchrs
: default space, can be a string of characters to be trimmed
- partition (self, ch)
-
partition the string using first occurance of a delimiter
Parameters:
self
: the stringch
: delimiter
Returns:
- part before ch
- ch
- part after ch
- rpartition (self, ch)
-
partition the string p using last occurance of a delimiter
Parameters:
self
: the stringch
: delimiter
Returns:
- part before ch
- ch
- part after ch
- at (self, idx)
-
return the ‘character’ at the index.
Parameters:
self
: the stringidx
: an index (can be negative)
Returns:
-
a substring of length 1 if successful, empty string otherwise.
- lines (self)
-
return an interator over all lines in a string
Parameters:
self
: the string
Returns:
-
an iterator
- title (self)
-
iniital word letters uppercase (‘title case’).
Here ‘words’ mean chunks of non-space characters.
Parameters:
self
: the string
Returns:
-
a string with each word’s first letter uppercase
- shorten (self, sz, tail)
-
return a shorted version of a string.
Parameters:
self
: the stringsz
: the maxinum size allowedtail
: true if we want to show the end of the string (head otherwise)