Clojure Koans: Strings

Hey guys, In this video Im going to go over the next Clojure Koan which goes over strings. In this video im going to be using Clojure Docs to look up functions to make sure im doing everything correctly, not that clojure koans wont tell me

lein koan run

As you can see, we are on the second clojure koan and its all about strings

“A string is nothing more than text surrounded by double quotes”
(= __ “hello”)


“But double quotes are just magic on top of something deeper”
(= __ (str ‘world))

Must be “world”. You can evaluate a simple by adding a tick or a single apostrophe.

“You can do more than create strings, you can put them together”
(= “Cool right?” (str __ __))

“Cool ” “right?” remember your whitespace ;)

“You can even get certain characters”
(= \C (get “Characters” __))

get returns a value mapped to key, not-found or nil if not present. So to get \c we have to pass in the index 0.

“Or even count the characters”
(= __ (count “Hello World”))

Count seems like an easy function but looking at the docs is always a good idea! Count returns the number of items in the collection. So a string is just a collections. Thats the cool thing about clojure is that almost everything can be evaluated as a collection. Theres probably some cases that I am not aware of. 11 chars

“But strings and characters are not the same”
(= __ (= \c “c”))


“What if you only wanted to get part of a string?”
(= “World” (subs “Hello World” __ __))

Subs returns the substring of s beginning at start inclusive and ending at end. Pass in the index of 6 because we want “world” of “Hello world”.

“How about joining together elements in a list?”
(= __ (string/join ‘(1 2 3)))

“123” String/join returns a string of all elements in coll, as returned by (seq coll), separated by an optional separator.

“What if you wanted to separate them out?”
(= “1, 2, 3” (string/join __ ‘(1 2 3)))

“, ” If you want to add anything between each element in the collection.

“Maybe you want to separate out all your lines”
(= [__ __ __] (string/split-lines “1\n2\n3”))

“1” “2” “3” Split-lines splits the string on \n or \r\n.

“You may want to make sure your words are backwards”
(= __ (string/reverse “hello”))

“olleh” Reverse string seems fairly straight forward.

“Maybe you want to find the index of the first occurence of a substring”
(= 0 (.indexOf “hello world” __))

If anyone knows where I can find .indexOf. (After a quick search, it looks like .indexOf is a Java interop.)

“Maybe you want to find the index of the first occurence of a substring”
(= 0 (.indexOf “hello world” __))

The 0 index will be “h”

“Or maybe the last index of the same”
(= __ (.lastIndexOf “hello world, hello” “hello”))


“But when something doesn’t exist, it turns up negative”
(= __ (.indexOf “hello world” “bob”))


“Sometimes you don’t want whitespace cluttering the front and back”
(= __ (string/trim “ \nhello world \t \n”))

“hello world”, Trim removes whitespace from both ends of string.

“You can check if something is a char”
(= __ (char? \c))


“But it may not be”
(= __ (char? “a”))

false, a char is not a string

“But chars aren’t strings”
(= __ (string? \b))


“Strings are strings”
(= true (string? __))


“Some strings may be blank”
(= __ (string/blank? “”))


“Even if at first glance they aren’t”
(= __ (string/blank? “ \n \t “))


“However, most strings aren’t blank”
(= __ (string/blank? “hello?\nare you out there?”))


It looks like that concludes our Strings Koan.

Until next time!

Checkout my next one on lists:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.