PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/clojure_koans/strings.clj

https://gitlab.com/jimador/clojure-studies
Clojure | 68 lines | 46 code | 22 blank | 0 comment | 23 complexity | 4ab356f9b7e5e5debebebf6964a28d5d MD5 | raw file
  1. (ns clojure-koans.strings
  2. (:require [clojure.string :as string]))
  3. "A string is nothing more than text surrounded by double quotes"
  4. (= "hello" "hello")
  5. "But double quotes are just magic on top of something deeper"
  6. (= "world" (str 'world))
  7. "You can do more than create strings, you can put them together"
  8. (= "Cool right?" (str "Cool " "right?"))
  9. "You can even get certain characters"
  10. (= \C (get "Characters" 0))
  11. "Or even count the characters"
  12. (= 11 (count "Hello World"))
  13. "But strings and characters are not the same"
  14. (= false (= \c "c"))
  15. "What if you only wanted to get part of a string?"
  16. (= "World" (subs "Hello World" 6 11))
  17. "How about joining together elements in a list?"
  18. (= "123" (string/join '(1 2 3)))
  19. "What if you wanted to separate them out?"
  20. (= "1, 2, 3" (string/join ", " '(1 2 3)))
  21. "Maybe you want to separate out all your lines"
  22. (= ["1" "2" "3"] (string/split-lines "1\n2\n3"))
  23. "You may want to make sure your words are backwards"
  24. (= "olleh" (string/reverse "hello"))
  25. "Maybe you want to find the index of the first occurence of a substring"
  26. (= 0 (string/index-of "hello world" "h"))
  27. "Or maybe the last index of the same"
  28. (= 13 (string/last-index-of "hello world, hello" "hello"))
  29. "But when something doesn't exist, nothing is found"
  30. (= nil (string/index-of "hello world" "bob"))
  31. "Sometimes you don't want whitespace cluttering the front and back"
  32. (= "hello world" (string/trim " \nhello world \t \n"))
  33. "You can check if something is a char"
  34. (= true (char? \c))
  35. "But it may not be"
  36. (= false (char? "a"))
  37. "But chars aren't strings"
  38. (= false (string? \b))
  39. "Strings are strings"
  40. (= true (string? "true"))
  41. "Some strings may be blank"
  42. (= true (string/blank? ""))
  43. "Even if at first glance they aren't"
  44. (= true (string/blank? " \n \t "))
  45. "However, most strings aren't blank"
  46. (= false (string/blank? "hello?\nare you out there?"))