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
String Predicates
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?. |
startswith (self, s2) | does string start with the substring?. |
endswith (s, send) | does string end with the given substring?. |
Strings and Lists
join (self, seq) | concatenate the strings using this string as a delimiter. |
splitlines (self, keepends) | break string into a list of lines |
split (self, re, n) | split a string into a list of strings using a delimiter. |
expandtabs (self, n) | replace all tabs in s with n spaces. |
Finding and Replacing
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. |
count (self, sub) | count all instances of substring in string. |
Stripping and Justifying
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. |
Partioning Strings
splitv (self, re) | split a string using a pattern. |
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. |
Miscelaneous
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. |
String Predicates
String Predicates- 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
- startswith (self, s2)
-
does string start with the substring?.
Parameters:
- self the string
- s2 a string
- endswith (s, send)
-
does string end with the given substring?.
Parameters:
- s a string
- send a substring or a table of suffixes
Strings and Lists
Strings and Lists- join (self, seq)
-
concatenate the strings using this string as a delimiter.
Parameters:
- self the string
- seq a table of strings or numbers
Usage:
(' '):join {1,2,3} == '1 2 3'
- splitlines (self, keepends)
-
break string into a list of lines
Parameters:
- self the string
- keepends (currently not used)
- split (self, re, n)
-
split a string into a list of strings using a delimiter.
Parameters:
- self the string
- re 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'}
- 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 string
- n number of spaces to expand each tab, (default 8)
Finding and Replacing
Finding and Replacing- lfind (self, sub, i1)
-
find index of first instance of sub in s from the left.
Parameters:
- self the string
- sub substring
- i1 start index
- rfind (self, sub, first, last)
-
find index of first instance of sub in s from the right.
Parameters:
- self the string
- sub substring
- first first index
- last 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 string
- old the target substring
- new the substitution
- n optional maximum number of substitutions
Returns:
- result string
- the number of substitutions
- count (self, sub)
-
count all instances of substring in string.
Parameters:
- self the string
- sub substring
Stripping and Justifying
Stripping and Justifying- ljust (self, w, ch)
-
left-justify s with width w.
Parameters:
- self the string
- w width of justification
- ch padding character, default ' '
- rjust (s, w, ch)
-
right-justify s with width w.
Parameters:
- s the string
- w width of justification
- ch padding character, default ' '
- center (s, w, ch)
-
center-justify s with width w.
Parameters:
- s the string
- w width of justification
- ch padding character, default ' '
- lstrip (self, chrs)
-
trim any whitespace on the left of s.
can be a string of characters to be trimmed
Parameters:
- self the string
- chrs default any whitespace character (%s),
- rstrip (s, chrs)
-
trim any whitespace on the right of s.
can be a string of characters to be trimmed
Parameters:
- s the string
- chrs default any whitespace character (%s),
- strip (self, chrs)
-
trim any whitespace on both left and right of s.
can be a string of characters to be trimmed
Parameters:
- self the string
- chrs default any whitespace character (%s),
Partioning Strings
Partioning Strings- 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)
Returns:
-
the parts of the string
Usage:
a,b = line:splitv('=')
- partition (self, ch)
-
partition the string using first occurance of a delimiter
Parameters:
- self the string
- ch 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 string
- ch delimiter
Returns:
- part before ch
- ch
- part after ch
- at (self, idx)
-
return the 'character' at the index.
Parameters:
- self the string
- idx an index (can be negative)
Returns:
-
a substring of length 1 if successful, empty string otherwise.
Miscelaneous
Miscelaneous- 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 string
- sz the maxinum size allowed
- tail true if we want to show the end of the string (head otherwise)