This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

How do I check if a string is a number in Java

How do I check if a string is a number before parsing it?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

Use Apache Commons Lang 3.5 and later: NumberUtils.iscreatable or Stringutils.isnumeric.

Use Apache Commons Lang 3.4 and below: NumberUtils.isNumber or Stringutils.isnumeric.

You can also use the StringUtils. IsNumericSpacewhich returns true empty string, and ignores the internal Spaces in the string. Another method is to use numberUtils.isparsable, a method that checks whether numbers are parsable based on Java basics. (The linked Javadocs contains detailed examples of each method.)

Answer:

This is usually done using a simple user-defined function (that is, the “isNumeric” function with its own scroll).

Like:

public static boolean isNumeric(String str) { try { Double.parseDouble(str); return true; } catch(NumberFormatException e){ return false; }}Copy the code

However, if you call this function multiple times and expect many checks to fail because they are not numbers, the mechanism will not perform well because you rely on an exception being thrown every time you fail, which is quite an expensive operation.

An alternative is to use regular expressions to check validity as numbers:

public static boolean isNumeric(String str) { return str.matches("-? \\d+(\\.\\d+)?" ); //match a number with optional '-' and decimal. }Copy the code

However, use the aforementioned RegEx mechanism with caution, because it will fail if you use non-Arabic numerals (that is, numbers other than 0 to 9). This is because the “\ D” part of the RegEx only matches [0-9] and has no actual international number awareness. (Thanks OregonGhost for pointing this out!)

Even an alternative is to use Java’s built-in java.text.NumberFormat object to see if the position of the parser is at the end of the string after parsing it. If so, we can assume that the entire string is a number:

public static boolean isNumeric(String str) {
  ParsePosition pos = new ParsePosition(0);
  NumberFormat.getInstance().parse(str, pos);
  return str.length() == pos.getIndex();
}
Copy the code

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…

I also prefer stringutils.isnumeric


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️