Checking if String contains a numeric value in Java

How to write a method to determine if a given string contains a number? The method should return true if the string contains a number and false otherwise.

+45
java
Jun 14 2018-11-11T00:
source share
8 answers
if(str.matches(".*\\d.*")){ // contains a number } else{ // does not contain a number } 

Previous proposed solution that does not work, but returned due to request / suggestion @ Eng.Fouad.

Broken proposed solution

 String strWithNumber = "This string has a 1 number"; String strWithoutNumber = "This string does not have a number"; System.out.println(strWithNumber.contains("\d")); System.out.println(strWithoutNumber.contains("\d")); 

Working solution

 String strWithNumber = "This string has a 1 number"; if(strWithNumber.matches(".*\\d.*")){ System.out.println("'"+strWithNumber+"' contains digit"); } else{ System.out.println("'"+strWithNumber+"' does not contain a digit"); } String strWithoutNumber = "This string does not have a number"; if(strWithoutNumber.matches(".*\\d.*")){ System.out.println("'"+strWithoutNumber+"' contains digit"); } else{ System.out.println("'"+strWithoutNumber+"' does not contain a digit"); } 

Exit

 'This string has a 1 number' contains digit 'This string does not have a number' does not contain a digit 
+83
Jun 14. '11 at 14:20
source share
 public static boolean containsDigit(final String s) { if (s != null && !s.isEmpty()) { for (char c : s.toCharArray()) { if (Character.isDigit(c)) { return true; } } } return false; } 

Literature:

+30
Jun 14 2018-11-11T00:
source share

The answer to the chef does not compile for me. It looks like it uses RegEx in String.contains() . If you want to use RegEx, use this:

 String strWithNumber = "This string has a 1 number"; String strWithoutNumber = "This string has a number"; System.out.println(strWithNumber.matches(".*\\d.*")); System.out.println(strWithoutNumber.matches(".*\\d.*")); 
+5
Jun 14 2018-11-11T00:
source share

It seems like people like to make a spoon, so I decided to post the worst solution for an easy task:

 public static boolean isNumber(String s) throws Exception { boolean result = false; byte[] bytes = s.getBytes("ASCII"); int tmp, i = bytes.length; while (i >0 && (result = ((tmp = bytes[--i] - '0') >= 0) && tmp <= 9)); return result; } 

About the worst code I could imagine, but there may be other people who can come up with even worse solutions.

Hm, containsNumber is worse:

 public static boolean containsNumber(String s) throws Exception { boolean result = false; byte[] bytes = s.getBytes("ASCII"); int tmp, i = bytes.length; while (i >0 && (true | (result |= ((tmp = bytes[--i] - '0') >= 0) && tmp <= 9))); return result; } 
+4
Jun 14 2018-11-11T00:
source share

You can use regular expression to find out if a string contains a number. Take a look at the matches() method.

+3
Jun 14 2018-11-11T00:
source share

Why aren't you trying to write a function based on Integer.parseInt(String obj) ? The function can take your String object as a parameter, and then tokenize the String and use Integer.parseInt(String obj) to extract the number from the "successful" substring ...

Javadoc of Integer.parseInt (String obj)

+2
Jun 14 2018-11-11T00:
source share
  try{ Integer.parseInt(string); return true; }catch (Exception e){ return false; } 

or you can do it yourself:

  for ( int i = 0; i < string.length; ++i ) { if ( !( string[i] >= '0' || string[i] <= '9' ) ) return false; } return true; 

Of course also the isDigit function

+1
Jun 14 2018-11-11T00:
source share

Another possible solution is to use a Scanner object as follows:

 Scanner scanner = new Scanner(inputString); if (scanner.hasNextInt()) { return true; } else { return false } 

Of course, if you are looking for double , use the hasNextDouble() method (see javadoc Scanner )

-2
Jun 14 2018-11-11T00:
source share



All Articles