How can I check a string contain a-z?
Do you want to check if the string contains at least one lowercase alphabet character?
my_string.chars().any(|c| matches!(c, 'a'..='z'))
some_string.bytes().all(|b| matches!(b, b'a'..=b'z'))
Yes! It work !
Thanks so much!
Yeah, I got it!
Thanks for your replay!
I need check contain any letter, like:
"1,2,3" => false
"1,2,3,a" => true
char::is_ascii_lowercase() not work
In this case, you want
any instead of
let s = "1,2,3";
let a = s.chars().any(|c| c.is_ascii());
a is still true
You are probably looking for
char::is_ascii_alphanumeric(). I'd suggest having a skim through the various
char::is_* methods because the docs tell you the exact characters they'll match.
In this case the comma (
,) and the arabic numerals (
9) are all valid ascii characters, so
s.chars().any(|c| c.is_ascii()) will correctly return
Thanks so much !
char::is_ascii_alphanumeric() work !
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.