Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - Check if every character in string is lowercase or space

How do I check if every character in a string is lowercase or space?

"this is all lowercase"   -> true 
"THIS is Some uppercase " -> false 
"HelloWorld  "             -> false 
"hello world"              -> true
like image 417
Tech Labradoodle Avatar asked Dec 17 '22 21:12

Tech Labradoodle


2 Answers

You could use the predicate/itr method of all (docs:

julia> f(s) = all(c->islower(c) | isspace(c), s);

julia> f("lower and space")
true

julia> f("mixED")
false
like image 81
DSM Avatar answered Apr 07 '23 21:04

DSM


You could also use regexes. The regex ^[a-z\s]+$ checks if you only have lowercase letters or spaces from start to end in your string.

julia> f(s)=ismatch(r"^[a-z\s]+$",s)
f (generic function with 1 method)

julia> f("hello world!")
false

julia> f("hello world")
true

julia> f("Hello World")
false
like image 42
niczky12 Avatar answered Apr 07 '23 22:04

niczky12