String is alphabetic or alphanumeric
- is_alphabetic
- is_alphanumeric
- all
- chars
The char type has methods such as is_alphabetic and is_alphanumeric and several other similar methods.
fn main() {
let strings = vec!["text", "t xt", "t,xt", "😇😈", "Ωأㅏñ", "שלום"];
for text in &strings {
println!("{}: {}", text, text.chars().all(|chr| chr.is_alphabetic()));
}
println!();
for text in &strings {
println!("{}: {}", text, text.chars().all(char::is_alphabetic));
}
println!();
for text in strings {
println!(
"{}: {}",
text,
text.chars().all(|chr| chr.is_alphabetic() || chr == ' ')
);
}
}
text: true
t xt: false
t,xt: false
😇😈: false
Ωأㅏñ: true
שלום: true
text: true
t xt: false
t,xt: false
😇😈: false
Ωأㅏñ: true
שלום: true
text: true
t xt: true
t,xt: false
😇😈: false
Ωأㅏñ: true
שלום: true