superstring.core

ascii?

(ascii? s)
Return s if s only contains ASCII characters.

blank?

(blank? s)
True if s is nil, empty, or contains only whitespace.

camel-case

(camel-case s)
Lower case the first char in s and use capitalization to separate words.

foo bar => fooBar
camelCase => camelCase
PascalCase => pascalCase

capitalize

(capitalize s)
Converts first character of the string to upper-case, all other
characters to lower-case.

center

(center s width)(center s width padding)
Pad both ends of s with padding, or spaces, until the length of s
matches width.

char-at

added in 1.1

(char-at s i)
Get the character in s at index i.

chomp

(chomp s)(chomp s separator)
Return a new string with the given record separator removed from
the end (if present).

If separator is not provided, chomp will remove \n, \r or \r\n from
the end of s.

chop

(chop s)
Return a new string with the last character removed.

If the string ends with \r\n, both characters are removed.

Applying chop to an empty string is a no-op.

chomp is often a safer alternative, as it leaves the string
unchanged if it doesn’t end in a record separator.

chop-prefix

(chop-prefix s prefix)(chop-prefix s prefix ignore-case)
If found, remove prefix from the start of s.

Otherwise return s.

chop-suffix

(chop-suffix s suffix)(chop-suffix s suffix ignore-case)
If found, remove suffix from the end of s.

Otherwise return s.

collapse-whitespace

(collapse-whitespace s)
Convert all adjacent whitespace in s to a single space.

common-prefix

(common-prefix s1 s2)(common-prefix s1 s2 ignore-case)
Return the longest common prefix of s1 and s2.

(common-prefix "abadon" "aberdeen") => "ab"
(common-prefix "foo" "bar") => ""

common-suffix

(common-suffix s1 s2)(common-suffix s1 s2 ignore-case)
Return the longest common suffix of s1 and s2.

(common-suffix "bba" "aba") => "ba"
(common-suffix "foo" "bar") => ""

distance

(distance s1 s2)(distance s1 s2 algorithm)
Get the edit distance between s1 and s2.

The default distance metric is the Levenshtein distance.

The optional algorithm argument can be either :levenshtein to get
the default, or :hamming to get the Hamming distance between s1 and
s2.

ends-with?

(ends-with? s suffix)(ends-with? s suffix ignore-case)
Return s if s ends with suffix.

If a third argument is provided the string comparison is insensitive to case.

escape

(escape s cmap)
Return a new string, using cmap to escape each character ch
from s as follows:

If (cmap ch) is nil, append ch to the new string.
If (cmap ch) is non-nil, append (str (cmap ch)) instead.

includes-all?

(includes-all? s needles)(includes-all? s needles ignore-case)
Return s if s includes all needles.

(includes-all? "foo bar baz" ["foo" "bar"]) => "foo bar baz"
(includes-all? "foo bar" ["qux" "bar"]) => nil

includes-any?

(includes-any? s needles)(includes-any? s needles ignore-case)
Return s if s includes any of the needles.

(includes-any? "foo bar baz" ["foo" "qux"]) => "foo bar baz"
(includes-any? "foo bar" ["qux" "quux"]) => nil

includes?

(includes? s needle)(includes? s needle ignore-case)
Return s if s includes needle.

(includes? "foobar" "foo") => "foobar"
(includes? "foobar" "qux") => nil

index-of

(index-of s value)(index-of s value from-index)
Return index of value (string or char) in s, optionally searching
forward from from-index. Return nil if value not found.

join

(join coll)(join separator coll)
Returns a string of all elements in coll, as returned by (seq coll),
separated by an optional separator.

last-index-of

(last-index-of s value)(last-index-of s value from-index)
Return last index of value (string or char) in s, optionally
searching backward from from-index. Return nil if value not found.

length

(length s)
Return the length of s.

lisp-case

(lisp-case s)
Lower case s and separate words with dashes.

foo bar => foo-bar
camelCase => camel-case

This is also referred to as kebab-case in some circles.

longest-common-substring

(longest-common-substring s1 s2)
Returns the set of the longest common substrings in s1 and s2.

This implementation uses dynamic programming, and not a generalized
suffix tree, so the runtime is O(nm).

lower-case

(lower-case s)
Converts string to all lower-case.

lower-case?

(lower-case? s)
Return s if s is all lower case.

Characters without case, e.g. numbers, are considered to be trivially
lower case.

mixed-case?

(mixed-case? s)
Return s if s contains both upper and lower case letters.

(mixed-case? "foo1") => nil
(mixed-case? "Foo Bar") => "Foo Bar"

pad-left

(pad-left s width)(pad-left s width padding)
Pad the beginning of s with padding, or spaces, until the length of
s matches width.

pad-right

(pad-right s width)(pad-right s width padding)
Pad the end of s with padding, or spaces, until the length of s matches
width.

pascal-case

(pascal-case s)
Upper the case first char in s and use capitalization to separate words.

foo bar => FooBar
camelCase => CamelCase
PascalCase => PascalCase

re-quote

added in 1.1

(re-quote s)
Create a string matching s exactly, and nothing else, for use in
regular expressions.

re-quote-replacement

(re-quote-replacement replacement)
Given a replacement string that you wish to be a literal
replacement for a pattern match in replace or replace-first, do the
necessary escaping of special characters in the replacement.

replace

(replace s match replacement)
Replaces all instance of match with replacement in s.

match/replacement can be:

string / string
char / char
pattern / (string or function of match).

See also replace-first.

The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.

For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern.  If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument.  See also documentation for
java.util.regex.Matcher's appendReplacement method.

Example:
(clojure.string/replace "Almost Pig Latin" #"\b(\w)(\w+)\b" "$2$1ay")
-> "lmostAay igPay atinLay"

replace-first

(replace-first s match replacement)
Replaces the first instance of match with replacement in s.

match/replacement can be:

char / char
string / string
pattern / (string or function of match).

See also replace.

The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.

For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern.  If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument.  See also documentation for
java.util.regex.Matcher's appendReplacement method.

Example:
(clojure.string/replace-first "swap first two words"
                              #"(\w+)(\s+)(\w+)" "$3$2$1")
-> "first swap two words"

replace-last

added in 3.0

(replace-last s match replacement)
Like `replace-first`, but replaces the last occurrence instead of the first.

reverse

(reverse s)
Returns s with its characters reversed.

screaming-snake-case

(screaming-snake-case s)
Upper case s and use underscores to separate words.

foo bar => FOO_BAR
camelCase => CAMEL_CASE
PascalCase => PASCAL_CASE

slice

(slice s index)(slice s index length)
Return a slice of s beginning at index and of the given length, or 1.

If index is negative, the starting index is relative to the end of the string.

If the requested slice ends outside the string boundaries, we return
the substring of s starting at index.

Returns nil if index falls outside the string boundaries or if
length is negative.

slug

(slug s)
Transform s so it's suitable for use in URLs.

The following transformations are applied:

* Diacritical marks are removed from all characters.
* Any character which isn't alphanumeric or in #{_-.~} is removed.
* Lower case
* Whitespace is collapsed and replaced by a single dash.

snake-case

(snake-case s)
Lower case s and use underscores to separate words.

foo bar => foo_bar
camelCase => camel_case
PascalCase => pascal_case

some?

added in 3.0

(some? s)
Complement of `blank?`

split

(split s re)(split s re limit)
Splits string on a regular expression.  Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.

split-lines

(split-lines s)
Splits s on \n or \r\n.

starts-with?

(starts-with? s prefix)(starts-with? s prefix ignore-case)
Return s if s starts with prefix.

If a third argument is provided the string comparison is insensitive to case.

strip-accents

(strip-accents s)
Strip all accents (diacritical marks) from s.

Et ça sera sa moitié => Et ca sera sa moitie

substring

(substring s start)(substring s start end)
Returns the substring of s beginning at start inclusive, and ending
at end (defaults to length of string), exclusive.

swap-case

(swap-case s)
Change lower case characters to upper case and vice versa.

translate

(translate s tmap)(translate s tmap delete-chars)
Translate all characters in s according to the mappings found in tmap.

Any characters found in the set delete-chars will be pruned prior to
consulting tmap.

Any characters mapping to nil in tmap will also be deleted.

(translate "abba" {\a \b}) => bbbb
(translate "abba" {\a \b, \b \a}) => baab
(translate "foo" {\a }) =>  foo
(translate "gabba" {\a \b} #{\b}) => gbb
(translate "gabba" {\a nil} #{\b}) => g

trim

(trim s)
Removes whitespace from both ends of string.

trim-newline

(trim-newline s)
Removes all trailing newline \n or return \r characters from
string.  Similar to Perl's chomp.

triml

(triml s)
Removes whitespace from the left side of string.

trimr

(trimr s)
Removes whitespace from the right side of string.

truncate

(truncate s len)
If s is longer than len-3, cut it down to len-3 and append '...'.

upper-case

(upper-case s)
Converts string to all upper-case.

upper-case?

(upper-case? s)
Return s if s is all upper case.

Characters without case, e.g. numbers, are considered to be trivially
upper case.

wrap-words

(wrap-words s width)
Insert newlines in s so the length of each line doesn't exceed width.