When programming, we often encounter the need to determine whether the characters in a string are numbers (0-9) and whether the string is a number. You may use some of the methods that come with Java, or you may use other methods. Weird tricks, such as judging whether it is an integer number and forcing the string to be an integer. If it is not a number, an error will be thrown, so it is not an integer. Here I will introduce you to several implementation methods.
How to judge whether it is a number in java
1. Use Character.isDigit(char) to judge (only one can be judged Characters)
String str = "123abc"; if (!"".equals(str)) { char num[] = str.toCharArray(); //把字符串转换为字符数组 StringBuffer title = new StringBuffer(); //使用StringBuffer类,把非数字放到title中 StringBuffer hire = new StringBuffer(); //把数字放到hire中 for (int i = 0; i < num.length; i++) { // 判断输入的数字是否为数字还是字符 if (Character.isDigit(num[i])) { 把字符串转换为字符, 再调用Character.isDigit(char) 方法判断是否是数字, 是返回True, 否则False hire.append(num[i]); // 如果输入的是数字,把它赋给hire } else { title.append(num[i]); // 如果输入的是字符,把它赋给title } } }
2. Use type conversion to determine
try { String str = "123abc"; int num = Integer.valueOf(str); //把字符串强制转换为数字 return true; //如果是数字,返回True } catch (Exception e) { return false; //如果抛出异常,返回False }
3. Use regular expressions to determine
String str = ""; boolean isNum = str.matches("[0-9]+"); //+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7") 4. 使用Pattern类和Matcher view plaincopy String str = "123"; Pattern pattern = Pattern.compile("[0-9]+"); Matcher matcher = pattern.matcher((CharSequence) str); boolean result = matcher.matches(); if (result) { System.out.println("true"); } else { System.out.println("false"); }
Recommended tutorial: java tutorial
The above is the detailed content of How to determine whether it is a number in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor