basilisp.string
String manipulation utilities.
The functions in this namespace generally are a thin wrapper around Python str
methods and may thus generally be assumed to have the same return values as the
corresponding methods.
- fn (alpha? s)
Return
true
ifs
is strictly alphabetic and there is at least one character.This function uses Python’s underlying
str.isalpha
and, thus, it respects unicode.
- fn (alphanumeric? s)
Return
true
ifs
is strictly alphanumeric and there is at least one character.This function uses Python’s underlying
str.isalnum
and, thus, it respects unicode.
- fn (blank? s)
Returns
true
ifs
isnil
, empty, or contains only whitespace.
- fn (capitalize s)
Return a copy of the string
s
with the first character capitalized and the rest lower case.
- fn (digits? s)
Return
true
ifs
is strictly digit characters and there is at least one character.
- fn (ends-with? s suffix)
Return
true
ifs
ends with the substringsuffix
.
- fn (includes? s substr)
Returns
true
ifsubstr
is contained ins
.
- fn (index-of s value)
- fn (index-of s value from-index)
Return the first index of value in
s
, optionally starting fromfrom-index
. Returnsnil
if value is not found ins
.
- fn (join coll)
- fn (join separator coll)
Return a string of the elements in
coll
joined together, optionally by aseparator
.
- fn (last-index-of s value)
- fn (last-index-of s value from-index)
Return the last index of value in
s
, optionally searching backwards fromfrom-index
. Returnsnil
if value is not found ins
.
- fn (lower-case s)
Return a copy of the string
s
with all characters converted to lower case.
- fn (lpad s width)
- fn (lpad s width fillchar)
Pad
s
on the left such that the final string length iswidth
. If the initial string length is less than or equal towidth
, return the original string. If afillchar
is specified, pad withfillchar
. Otherwise, use a space.
- fn (ltrim s)
Trim leading whitespace from
s
.
- fn (re-quote-replacement replacement)
Escape special characters in a regex replacement pattern so they are interpreted literally, rather than as special characters.
- fn (replace s match replacement)
Replace all instances of match in
s
withreplacement
.match
andreplacement
can be either:str
andstr
re.Pattern
and (str
or a function)
If
match
is a regex pattern andreplacement
is a function, that function will be called once for every non-overlapping occurrence of match. The function should accept one string argument and return a replacement string.If both
match
andreplacement
are strings, this function behaves as the Python builtinstr.replace
.
- fn (replace-first s match replacement)
Replace the first instance of match in
s
withreplacement
.match
andreplacement
can be either:str
andstr
re.Pattern
and (str
or a function)
If
match
is a regex pattern andreplacement
is a function, that function will be called once for every non-overlapping occurrence of match. The function should accept one string argument and return a replacement string.If both
match
andreplacement
are strings, this function behaves as the Python builtinstr.replace
with thecount
argument set to 1.
- fn (reverse s)
Returns a string which is the reverse of
s
.
- fn (rpad s width)
- fn (rpad s width fillchar)
Pad
s
on the right such that the final string length iswidth
. If the initial string length is less than or equal towidth
, return the original string. If afillchar
is specified, pad withfillchar
. Otherwise, use a space.
- fn (rtrim s)
Trim trailing whitespace from
s
.
- fn (split s pattern)
- fn (split s pattern limit)
Split a string on a regular expression or another string. Caller may optionally limit the maximum number of splits with
limit
. Returns a vector of the splits.
- fn (split-lines s)
Split
s
on universal newlines as by Python’sstr.splitlines
.
- fn (starts-with? s prefix)
Return
true
ifs
starts with the substringprefix
.
- fn (title-case s)
Return a copy of the string
s
where the first letter of each word is capitalized and the rest of the characters in the word are lower case.
- fn (trim s)
Trim whitespace off the ends of
s
.
- fn (trim-newlines s)
Trim trailing newline and return characters from
s
.
- fn (upper-case s)
Return a copy of the string
s
with all characters converted to upper case.