使用Lambda表达式检查字符串在Java中是否仅包含字母
假设我们的字符串是-
String str = "Amit123";
现在,使用allMatch()method来获取布尔结果,无论字符串是只有字母还是现在-
boolean result = str.chars().allMatch(Character::isLetter);
以下是使用Lambda表达式检查字符串是否仅包含字母的示例-
示例
class Main {
public static void main(String[] args) {
String str = "Amit123";
boolean result = str.chars().allMatch(Character::isLetter);
System.out.println("String contains only alphabets? = "+result);
}
}输出结果
让我们看另一个带有不同输入的示例-
String contains only alphabets? = false
示例
class Main {
public static void main(String[] args) {
String str = "Jacob";
boolean result = str.chars().allMatch(Character::isLetter);
System.out.println("String contains only alphabets? = "+result);
}
}输出结果
String contains only alphabets? = true
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短