检查字符串是否为android中的数字 [英] Check if string is number in android
本文介绍了检查字符串是否为android中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个文本字段,它们都是数字或十进制数字.
I have three text fields all which are numbers or a decimal number.
它们被接受为字符串,以后将被转换.
They are accepted as strings which are later converted.
但是,我想验证这些字段,以便仅接受数字或十进制数字.
However, i want to validate these fields so that only the numbers or decimal numbers are accepted.
确保这些字段仅为字符0-9的最简单方法是什么.
Whats the easiest way to ensure that these fields are only characters 0-9.
我希望使用if语句,但是任何解决方案都不错!
I would prefer an if statement but any solution would be nice!
推荐答案
使用 string.matches
方法,该方法接受正则表达式作为参数.
Use string.matches
method which accepts a regex as an argument.
if(string.matches("\\d+(?:\\.\\d+)?"))
{
System.out.println("Matches");
}
else
{
System.out.println("No Match");
}
这篇关于检查字符串是否为android中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文