Search code examples
clojurecountwords

Clojure - how to count specific words in a string


(def string "this is an example string. forever and always and and")

can somebody help me? I coding in Clojure, and I have been trying to count how many times the word 'and' appears in the string.

any help is much appreciated


Solution

  • One way to do it is to use regular expressions and re-seq function. Here is a "naive" example:

    (count (re-seq #"and" string))
    

    And here is the same code, written with treading macro ->>:

    (->> string
         (re-seq #"and")
         count)
    

    It will count all appearances of sub-string "and" in your string. It means that words like panda will be counted too. But we could count only for and words by adding some restrictions to the regular expression (using a "word boundary" metacharacter \b):

    (->> string
         (re-seq #"\band\b")
         count)
    

    This version will ensure that "and" sub-string is surrounded by non-letter characters.

    And if you want case-insensitive search (to include "And"):

    (->> string
         (re-seq #"(?i)\band\b")
         count)
    

    Alternative solution is to use split function from clojure.string namespace:

    (require '[clojure.string :as s])
    
    (->> (s/split string #"\W+") ; split string on non-letter characters
         (map s/lower-case) ; for case-insensitive search
         (filter (partial = "and"))
         count)