Java,如何检查字符串是否包含数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43292673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:18:20  来源:igfitidea点击:

Java, how to check if a string contains a digit?

java

提问by crl

how do I check if a string contains a digit?

如何检查字符串是否包含数字?

public static void main(String[] args)
{
    s1 = "Hello32"; //should be true`enter code here`
    s2 = "He2llo"; //should be true
    s3 = "Hello"; //should be false
}

回答by davidxxx

With a regex you could search at least a digit among any (zero or more) characters:

使用正则表达式,您可以在任何(零个或多个)字符中至少搜索一个数字:

boolean hasDigit = s1.matches(".*\d+.*");

回答by rishabhkeshari123

In java

在 Java 中

public boolean containsNumber(String string)

public boolean containsNumber(String string)

{

{

return string.matches(".*\d+.*");

}

}

回答by Hema

Check this it might help you

检查这个它可能对你有帮助

String regex = "\d+";
System.out.println("abc45hdg".matches(regex));