string
string provides string manipulation functions.
Constant functions
ascii_lowercase
$ ascii_lowercase -> str
ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"
This returns a string of all lowercase letters in the ASCII character set.
ascii_uppercase
$ ascii_uppercase -> str
ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
This returns a string of all uppercase letters in the ASCII character set.
ascii_letters
$ ascii_letters -> str
ascii_letters = . ascii_uppercase ascii_lowercase
This returns a string of all alphabetical characters in the ASCII character set, by concatenating the strings of lowercase and uppercase letters.
digits
$ digits -> str
digits = "0123456789"
This returns a string of all ten Arabic numerals.
Core functions
length
& length str -> whole
length s = len(s)
This returns the length of a string using Python's builtin len
function.
charAt
& charAt str whole -> str
charAt s n = s[n]
This returns the character at index n
in the string s
using Python's builtin string indexing capabilities. Indexing starts at 0.
Substring functions
substr
$ substr str whole whole -> str
substr string start end =
| error indexErr "Substring start index must be greater than or equal to zero" if < start 0
| error indexErr "Substring start index must be less than or equal to the end index" if > start end
| error indexErr "Substring end index must be less than or equal to the string length" if > end length string
| _produce_substr string "" start end 0 otherwise
This acts as a wrapper for _produce_substr
. It provides some error checking on the start and end indices, and calls _produce_substr
with the correct initial values. It returns the substring of string
from start
inclusive to end
exclusive.
_produce_substr
$ _produce_substr str str whole whole whole -> str
_produce_substr string substring start end n =
| substring if == end n
| _produce_substr string substring start end + n 1 if < n start
| produce_substr string . substring charAt string n start end + n 1 otherwise
This recursively produces a substring if called with the correct initial parameters. substr
provides the correct intial parameters for _produce_substr
to produce substrings.
Search functions
contains
$ contains str str -> bool
contains string substring = >= indexOf string substring 0
This returns True
if string
contains substring
, and False
otherwise. It compares the index of substring
in string
to 0 (if there is no match, the index will be -1, otherwise it will be 0 or greater, and this information is used to return either True
or False
).
indexOf
$ indexOf str str -> int
indexOf string substring = _find_index string substring length string length substring 0
This acts as a wrapper for _find_index
. It calls _find_index
with the correct initial values. It returns the start index of the first match of substring
in string
, or -1 if there is no match.
_find_index
$ _find_index str str int int int -> int
_find_index string substring str_len substr_len index =
| -1 if > + index substr_len str_len
| index if == substr string index + index substr_len substring
| _find_index string substring str_len substr_len + index 1 otherwise
This recursively searches for a given substring in a string if called with the correct intial parameters, and returns either the index of the first match of substring
in string
, or -1 if there is no match. indexOf
provides the correct initial parameters.