Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is number in 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.

Whats the easiest way to ensure that these fields are only characters 0-9.

I would prefer an if statement but any solution would be nice!

like image 500
user3411002 Avatar asked Feb 01 '15 12:02

user3411002


People also ask

How do I check if a string is number in Kotlin?

Using isDigit() and all() Here, we check if each character of a String is a numeric digit. The all method returns true if all characters return true when passed to the isDigit method.

How do you check if there is a number in a string Java?

Use the isDigit() Method to Check if String Contains Numbers in Java. To find an integer from a string, we can use this in-built function called isDigit() .


1 Answers

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");
}
like image 86
Avinash Raj Avatar answered Oct 01 '22 05:10

Avinash Raj