Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java/Android中只使用数字检查字符串_Java_Android - Fatal编程技术网

在Java/Android中只使用数字检查字符串

在Java/Android中只使用数字检查字符串,java,android,Java,Android,我有一个字符串,它有数字和文本。(数字也有十进制值。)我想检查字符串是否只包含带十进制值的数字 以下是字符串: String testValue = "One Time Password(OTP) for Debit Card ending 1234 is 359573. Pls use this OTP at HDFC Bank ATM(Language selection screen) to create your ATM PIN before 27Jan15" 我提取这个值是为了得到另

我有一个字符串,它有数字和文本。(数字也有十进制值。)我想检查字符串是否只包含带十进制值的数字

以下是字符串:

String testValue = "One Time Password(OTP) for Debit Card ending 1234 is 359573. Pls use this OTP at HDFC Bank ATM(Language selection screen) to create your ATM PIN before 27Jan15"
我提取这个值是为了得到另一段代码中的唯一数字。我将其存储在testValue中。有时我会得到上面这样的完整消息,我试图找到testValue是否只有数字…所以 我尝试了以下方法:

if (Pattern.matches("[a-zA-Z]+", testValue ) == false && testValue.length() > 2) 
{
  Log.e("True","Only numbers);                  
}
这是真的

if (testValue.contains("[0-9]+"))
{
    Log.e("True","Only numbers);
}
这也是事实

if (testValue.matches(".*\\d+.*")
{
        Log.e("True","Only numbers);
    }
这也是事实

if (testValue.matches(".*\\d+.*")
{
        Log.e("True","Only numbers);
    }
以上所有条件都是真的,我不知道为什么,因为它包含数字和字母

如何检查testValue是否只有数字(也只有小数)

谢谢

Pattern.matches(“[a-zA-Z]+”,testValue)==false
如果
testValue
不是所有字母,则为true

testValue.contains(“[0-9]+”)
如果
testValue
中至少有一个数字,则返回true

testValue.matches(“%d+.”)
如果在
testValue
中至少有一个数字,则也会返回true

要仅测试数字,请使用中间的数字,但使用
matches()

testValue.matches(“[0-9]+”)

如果您还希望允许一个可选的
,后面有一个或多个数字,您可以使用:

testValue.matches(“[0-9]+(\\.[0-9]+)?”)
String testValue=“以1234结尾的借记卡的一次性密码(OTP)为359573。请在2015年1月27日之前在HDFC银行ATM(语言选择屏幕)上使用此OTP创建您的ATM PIN”;
if(testValue.contains(“[a-zA-Z]+”)&(testValue.contains(“.”))
{
System.out.println(“仅限真数字”);
}
其他的
System.out.println(“假”);

}
使用TextUtil.isDigit()尝试使用TextUtil。这不是workuse TextUtils.isDigitsOnly();比我快+1对于每个错误的解释和问题的正确答案:)现在没有打印任何内容。即使是有数字的字符串。例如:1419496.58完美。我就在附近,但没法让它工作。无论如何谢谢你@KeppilI不需要文本。我需要检查字符串是否也只有带小数的数字。