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 if s 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 if s 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 if s is nil, 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 if s is strictly digit characters and there is at least one character.

fn (ends-with? s suffix)

Return true if s ends with the substring suffix.

fn (includes? s substr)

Returns true if substr is contained in s.

fn (index-of s value)
fn (index-of s value from-index)

Return the first index of value in s, optionally starting from from-index. Returns nil if value is not found in s.

fn (join coll)
fn (join separator coll)

Return a string of the elements in coll joined together, optionally by a separator.

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 from from-index. Returns nil if value is not found in s.

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 is width. If the initial string length is less than or equal to width, return the original string. If a fillchar is specified, pad with fillchar. 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 with replacement.

match and replacement can be either:

  • str and str

  • re.Pattern and (str or a function)

If match is a regex pattern and replacement 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 and replacement are strings, this function behaves as the Python builtin str.replace.

fn (replace-first s match replacement)

Replace the first instance of match in s with replacement.

match and replacement can be either:

  • str and str

  • re.Pattern and (str or a function)

If match is a regex pattern and replacement 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 and replacement are strings, this function behaves as the Python builtin str.replace with the count 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 is width. If the initial string length is less than or equal to width, return the original string. If a fillchar is specified, pad with fillchar. 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’s str.splitlines.

fn (starts-with? s prefix)

Return true if s starts with the substring prefix.

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.