In Clojure we can use the clojure.string/split
function to split a string, based on a regular expression, into a vector with string values. Optionally we can also specify a limit on the maximum number of returned string values we want. If we want to split a string based on the newline characters we can use the function clojure.string/split-lines
that returns a vector where each element is a line from the original multi-line string.
The following example shows several usages of the split
and split-lines
functions:
Continue reading →
We can search for a value in a string and replace it with another value using the clojure.string/replace
function. The first parameter is the original string value that we want to replace parts of. The second parameter can be a string value or regular expression. The last parameter is the replacement value that can be a string value or a function that returns a string value. The function itself gets either a string argument if the match has no nested groups (when match is a regular expression) or a vector with a complete match followed by the nested groups when the match has nested groups.
In the following example we several invocation of the clojure.string/replace
function with different arguments:
Continue reading →
In the clojure.string
namespace we can find several useful function for working with strings. If we want to trim a string we can choose for the trim
, trial
, trimr
and trim-newline
functions. To trim all characters before a string we must use the triml
function. To remove all space characters after a string we use trimr
. To remove space characters both before and after a string we can use the trim
function. Finally if we only want to remove the newline and/or return characters we use the trim-newline
function.
In the following example we use the different trim functions on strings:
Continue reading →