我有一个 if
语句,用于检查变量是否等于某个字符串。但是,我也想检查字符串中是否有数字。像这样的事情:
if(thestring.equals("I, am awesome. And I'm " + Somehowgetifthereisanumberhere + " years old")) {
//Do stuff
}
或者更具体地说,其中x是未知数字,只是为了知道那里有一个数字(任何数字):
String str = item.substring(item.indexOf("AaAaA" + x), item.lastIndexOf("I'm cool."));
如何做到这一点?
最佳答案
使用 regular expression :
if(thestring.matches("^I, am awesome. And I'm \\d+ years old$")) {
//Do stuff
}
关于java - 如何判断字符串中是否存在未知数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677128/