Julia - 检查字符串中的每个字符是否都是小写或 space

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

如何检查字符串中的每个字符都是小写还是 space?

"this is all lowercase"   -> true 
"THIS is Some uppercase " -> false 
"HelloWorld  "             -> false 
"hello world"              -> true

您可以使用 all 的 predicate/itr 方法(docs:

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

julia> f("lower and space")
true

julia> f("mixED")
false

您也可以使用正则表达式。正则表达式 ^[a-z,\s]+$ 检查字符串中是否只有小写字母或空格从头到尾。

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