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!
Instead of using the matches!()
macro you can also use the various methods on char
. The char::is_ascii_lowercase()
method would do the same thing.
some_string.chars().all(|c| c.is_ascii_lowercase())
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 all
.
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 (0
, 1
, .., 9
) are all valid ascii characters, so s.chars().any(|c| c.is_ascii())
will correctly return true
.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.