clojure

Clojure int to char (e.g. 1 to \1)


Is their a way in Clojure to go from an int to that int as a character, e.g. 1 to \1 ?

I have a string s and I want to parse out the digits that match a number n (n will always be 0 to 9)

e.g.

 (let [n 1]
     (filter #(= ??? %) "123123123"))

Where ??? would n as \n, e.g. 1 would return "111"

Or maybe there is a better way to filter a string to only instance of a single digit?


Solution

  • The "java" way:

    user=> (Character/forDigit 1 10) ; number and radix
    \1
    

    The "calculaty" way (add the int of \0 to it and then back to char):

    user=> (char (+ 1 (int \0)))
    \1