ClojureDocs

Nav

Namespaces

char

clojure.core

Available since 1.1 (source)
  • (char x)
Coerce to char
5 Examples
;; can coerce an int (or similar)
user=> (char 97)
\a

;; a byte can be coerced to a char
user=> (let [bytes-array (.getBytes "abc")]
         (char (first bytes-array)))
\a

;; char is just a function
user=> (map char [65 66 67 68])
(\A \B \C \D)
(->> [67 108 111 106 117 114 101]
     (map char)
     (apply str))

"Clojure"
;; backslash
user=> (char 92)
\\

;; String madness:
user=> (map str [\n \newline \\])
["n" "\n" "\\"]

;; convert back to int:
user=> (int \\)
92

;; whoops! this will expect additional input because it char's the bracket
user=> (char \)
   #=> 

;; don't you dare this!
user=> (char \ )
\space

;; make it readable
user=> (char \space)
\space

;; it gets uglier! Mark the text to see the difference
user=> \
   #=>

user=> \ 
\space

;; MaDnEsS!1!
user=> \\\\\\\\
\\
\\
\\
\\
;; string to code round trip
;; input string
(def in-string "鬼")
;; single char from string
(def in-char (.charAt in-string 0)) ;;=> \鬼
;; code from char
(def code (int in-char)) ;;=> 39740
;; char from code
(def out-char (char code)) ;;=> \鬼
;; back to input string
(def out-string (str out-char)) ;;=> "鬼"
(defn u-sub-char
  "If n is a single-digit integer in [0, ..., 9], returns the
  Unicode supscript character for n.  Returns nil for other numbers."
  [n]
  (if (and (>= n 0) (<= n 9))
    (char (+ 0x2080 n))
    nil))
See Also

Return true if x is a Character

Added by mmwaikar

Coerce to int

Added by Ljos

Casts to chars[]

Creates an array of chars

0 Notes
No notes for char