Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether a string is UpperCase or not in clojure?

I want to check whether a string is uppercase or not. There is a function to check for a character but no function to check it for a string.

like image 256
vishalAmbekar Avatar asked Sep 23 '16 07:09

vishalAmbekar


2 Answers

For this kind of question, start by looking for the Java answer :)

Is there an existing library method that checks if a String is all upper case or lower case in Java?

In this case, my recommandation is to use Apache Commons Lang's StringUtils/isAllUpperCase

(import org.apache.commons.lang3.StringUtils)

(StringUtils/isAllUpperCase "HeLLO")

If you want a solution portable across all platforms (Clojure, ClojureScript, ...) the best strategy is probably to compare the uppercased string to the original:

(require '[clojure.string :as str])

(defn all-uppercase? [s]
  (= s (str/upper-case s)))
like image 112
Valentin Waeselynck Avatar answered Sep 22 '22 01:09

Valentin Waeselynck


Assuming that you want to check that every character in String is uppercase, you can use every? like this:

user=> (every? #(Character/isUpperCase %) "Hello")
false
user=> (every? #(Character/isUpperCase %) "HELLO")
true
like image 33
ntalbs Avatar answered Sep 22 '22 01:09

ntalbs