Filtering alphabetic characters in Clojure

I want to filter out only alphabetic characters from the collection. For example, I want only the characters A B c d from "A(B%$c32d" . Is using regular expressions the only way?

+5
source share
4 answers

The simplest implementation I can think of:

 (filter (fn [x] (Character/isLetter x)) "abc") 

It uses the static Java function Character.isLetter .

+2
source

In Clojure:

 (apply str (filter #(Character/isLetter %) "abc:sQ/SDQ_")) ;; "abcsQSDQ" 

In Clojure and ClojureScript using regex:

  (apply str (re-seq #"[a-zA-Z]" "abc:sQ/SDQ_")) 
+6
source

Using regex is another solution:

 (re-seq #"[a-zA-Z]" "A(B%$c32d") 
+2
source

However, you did not specify much as the input string, the sequence is A, so you can filter the characters this way:

EDIT: ASCII Solution / Refinement

 (filter #(<= 65 (int %) 132) "abZ9%xy") ; See NielsK comment 

Returns a lazy character sequence.

-1
source

All Articles