[cuerdas "0.3.0"]
The missing clojure(script) string manipulation library.
La guitarra,
hace llorar a los sueños.
El sollozo de las almas
perdidas,
se escapa por su boca
redonda.
Y como la tarántula
teje una gran estrella
para cazar suspiros,
que flotan en su negro
aljibe de madera.
The simplest way to use cuerdas in a clojure project, is by including it in the dependency vector on your project.clj file:
[cuerdas "0.3.0"]
(ns my.namespace
(:require [cuerdas.core :as str]))
(str/strip-tags "<p>just <b>some</b> text</p>")
;; => "just some text"
(str/strip-tags "<p>just <b>some</b> text</p>" ["p"])
;; => "just <b>some</b> text"
Determines whether a string contains a substring.
(str/contains? "foobar" "bar")
;; => true
(str/contains? "foobar" nil)
;; => false
(str/contains? nil nil)
;; => false
Check if the string starts with prefix.
Alias: startswith?
(str/startswith? "foobar" "foo")
;; => true
(str/startswith? "foobar" nil)
;; => false
(str/startswith? nil "foo")
;; => false
Check if the string ends with suffix.
Alias: endswith?
(str/endswith? "foobar" "bar")
;; => true
(str/endswith? "foobar" nil)
;; => false
(str/endswith? nil "bar")
;; => false
Check if the string is empty.
(str/empty? "foobar")
;; => false
(str/empty? nil)
;; => true
(str/empty? "")
;; => true
(str/empty? " ")
;; => false
Check if the string is empty or contains only whitespaces.
(str/blank? "foobar")
;; => false
(str/blank? " ")
;; => true
(str/blank? "")
;; => true
(str/blank? nil)
;; => true
Converts string to all upper-case.
(str/upper "foobar")
;; => "FOOBAR"
(str/upper nil)
;; => nil
Removes whitespace or specified characters from both ends of string.
Alias: strip
(str/trim " foo ")
;; => "foo"
(str/trim "-foo-", "-")
;; => "foo"
(str/trim nil)
;; => nil
Removes whitespace or specified characters from left side of string.
Alias: lstrip
(str/ltrim " foo ")
;; => "foo "
(str/ltrim "-foo-", "-")
;; => "foo-"
(str/ltrim nil)
;; => nil
Removes whitespace or specified characters from right side of string.
Alias: rstrip
(str/rtrim " foo ")
;; => " foo"
(str/rtrim "-foo-", "-")
;; => "-foo"
(str/rtrim nil)
;; => nil
Remove prefix from string if it matches exactly or leave the string untouched.
(str/strip-prefix nil nil)
;; => nil
(str/strip-prefix "a" nil)
;; => "a"
(str/strip-prefix "-=a" "-=")
;; => "a"
Remove suffix from string if it matches exactly or leave the string untouched.
(str/strip-suffix nil nil)
;; => nil
(str/strip-suffix "a" nil)
;; => "a"
(str/strip-suffix "a=-" "=-")
;; => "a"
Converts all adjacent whitespace characters to a single space.
(str/collapse-whitespace "a\n\nb")
;; => "a b"
(str/collapse-whitespace nil)
;; => nil
Truncates a string to certain left and adds "…" if necesary. Making sure that the pruned string does not exceed the original length and avoid half-chopped words when truncating.
(str/prune "Hello World" 5)
;; => "Hello..."
(str/prune "Hello World" 8)
;; => "Hello..."
(str/prune "Hello World" 11 " (...)")
;; => "Hello (...)"
(str/prune nil 5)
;; => nil
Join strings together with given separator.
(str/join ["foo" "bar"])
;; => "foobar"
(str/join "," ["foo" "bar"])
;; => "foo,bar"
Splits a string on a separator a limited number of times. The separator can be a string or RegExp instance.
(str/split "1 2 3")
;; => ["1" "2" "3"]
(str/split "1 2 3" " ")
;; => ["1" "2" "3"])
(str/split "1 2 3" #"\s")
;; => ["1" "2" "3"]
(str/split "1 2 3" #"\s" 2)
;; => ["1" "2 3"]
(str/split nil)
;; => nil
Returns a seq of char strings from string.
(str/chars "bar")
;; => ["b" "a" "r"]
(str/chars nil)
;; => nil
Return a list of the lines in the string.
(str/lines "foo\nbar")
;; => ["foo" "bar"]
(str/lines nil)
;; => nil
Joins a list of strings with a newline separator. This operation is the opposite of lines.
(str/unlines ["foo" "nbar"])
;; => "foo\nbar"
(str/unlines nil)
;; => nil
Extracts a section of a string and returns a new string.
(str/slice "123" 1)
;; => "23"
(str/slice "1234" 1 3)
;; => "23"
(str/slice nil 1 3)
;; => nil
Replaces all instance of match with replacement in s.
(str/replace "aa bb aa" "aa" "kk")
;; => "kk bb kk"
(str/replace "aa bb aa" #"aa" "kk")
;; => "kk bb kk"
(str/replace nil #"aa" "kk")
;; => nil
Replaces first instance of match with replacement in s.
(str/replace-first "aa bb aa" "aa" "kk")
;; => "kk bb aa"
(str/replace-first "aa bb aa" #"aa" "kk")
;; => "kk bb aa"
(str/replace-first nil #"aa" "kk")
;; => nil
Surround a string with another string.
(str/surround "a" "-")
;; => "-a-"
(str/surround "a" "-^-")
;; => "-^-a-^-"
(str/surround nil "-^-")
;; => nil
Unsurround a string surrounded by another.
(str/unsurround "-a-" "-")
;; => "a"
(str/unsurround "-^-a-^-" "-^-")
;; => "a"
(str/unsurround nil "-")
;; => nil
Transforms string into URL slug.
(str/slugify "Un éléphant à l'orée du bois")
;; => "un-elephant-a-loree-du-bois"
(str/slugify nil)
;; => nil
Remove html tags from string.
(str/strip-tags "<p>just <b>some</b> text</p>")
;; => "just some text"
(str/strip-tags "<p>just <b>some</b> text</p>" ["p"])
;; => "just <b>some</b> text"
(str/strip-tags nil)
;; => nil
It also allows arbitrary replacements:
(str/strip-tags "<p>just<br>text</p>" {:br "\n"})
;; => "just\ntext"
(str/strip-tags "<p>just<br>text</p>" ["br"] {:br "\n"})
;; => "<p>just\ntext</p>"
Trim and replace multiple spaces with a single space.
(str/clean " a b ")
;; => "a b"
(str/clean nil)
;; => nil
Takes a string and replaces newlines with a space. Multiple lines are replaced with a single space.
(str/strip-newlines "a\n\nb")
;; => "a b"
(str/strip-newlines nil)
;; => nil
General purpose function for parse number like strings to number. It works with integers and floats.
(str/parse-number "1.4")
;; => 1
(str/parse-number "1.4" 1)
;; => 1.4
(str/parse-number "1" 2)
;; => 1
(str/parse-number "")
;; => NaN
only on clojurescript |
Returns a float value. Wraps parseFloat.
(str/parse-float "1.4")
;; => 1.4
(str/parse-float "1")
;; => 1.0
(str/parse-float nil)
;; => NaN
only on clojurescript |
Returns a number value in integer form. Wraps parseInt.
(str/parse-int "1.4")
;; => 1
(str/parse-int nil)
;; => NaN
only on clojurescript |
Returns a number value in integer form. Wraps parseInt.
(str/parse-double "1.4")
;; => 1.4
(str/parse-double nil)
;; => NaN
only on clojure |
Returns a number value in integer form. Wraps parseInt.
(str/parse-long "1.4")
;; => 1
(str/parse-long nil)
;; => NaN
only on clojure |
Simple string interpolation.
(str/format "hello %s" "yen")
;; => "hello yen"
(str/format "hello %(name)s" {:name "yen"})
;; => "hello yen"
Pads the str with characters until the total string length is equal to the passed length parameter.
By default, pads on the left with the space char.
(str/pad "1" {:length 8})
;; => " 1"
(str/pad nil {:length 8})
;; => nil
(str/pad "1" {:length 8 :padding "0"})
;; => "00000001"
(str/pad "1" {:length 8 :padding "0" :type :right})
;; => "10000000"
(str/pad "1" {:length 8 :padding "0" :type :both})
;; => "00001000"
Converts first letter of the string to uppercase.
(str/capitalize "foo")
;; => "Foo"
(str/capitalize nil)
;; => nil
Converts a string from selector-case to camelCase.
(str/camelize "foo bar")
;; => "fooBar"
(str/camelize nil)
;; => nil
Converts a underscored or camelized string into an dasherized one.
(str/dasherize "MozTransform")
;; => "-moz-transform"
(str/dasherize nil)
;; => nil
Converts a camelized or dasherized string into an underscored one.
(str/underscored "MozTransform")
;; => "moz_transform"
(str/underscored nil)
;; => nil
Converts a string into TitleCase.
(str/titleize "my name is epeli")
;; => "My Name Is Epeli"
(str/titleize nil)
;; => nil
Converts string to camelized class name. First letter is always upper case.
(str/classify "some_class_name")
;; => "SomeClassName"
(str/classify nil)
;; => nil
Converts an underscored, camelized, or dasherized string into a humanized one.
(str/humanize " capitalize dash-CamelCase_underscore trim ")
;; => "Capitalize dash camel case underscore trim"
(str/humanize nil)
;; => nil
Escape characters on the string that are not safe to use in a RegExp.
(str/escape-regexp "\s")
;; => "\\s"
cuerdas has splitted implementation for clojure and clojurescript, but tests are build using speclj and cljx for execute the same tests for both implementations.
For run tests, cljx source should be compiled.
Additional note for run clojurescript tests: you should have instaled phantomjs.
$ lein cljx auto
$ lein spec -a
$ lein cljsbuild auto dev
cuerdas unlike Clojure and other Clojure contrib libs, does not have many restrictions for contributions.
Pull requests are welcome!
cuerdas is licensed under BSD (2-Clause) license:
Copyright (c) 2014-2015, Andrey Antukh <niwi@niwi.be> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close