本篇文章給大家分享的是有關(guān)Java判斷字符是否是數(shù)字的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。
一、使用自帶函數(shù)isDigit()
語(yǔ)法:
public static boolean isDigit(char ch)
實(shí)例:
public static boolean isNumeric (String str) { for (int i = str.length(); --i >=0) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true;}
二、使用正則表達(dá)式
實(shí)例1:
public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches();}
實(shí)例2:
public static boolean isNumeric(String str) { if (str != null && !"".equals(str.trim())) { return s.matches("^[0-9]*$"); } return false;}
實(shí)例3:
public static boolean isNumeric (String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matcher();}
三、使用ASCII碼
public static boolean isNumeric (String str) { for (int i = str.length(); --i>=0;) { int chr = str.charAt(i); if (chr < 48 || chr > 57) { return false; } } return true;}
以上就是Java判斷字符是否是數(shù)字的方法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:Java判斷字符是否是數(shù)字的方法-創(chuàng)新互聯(lián)
文章路徑:http://www.rwnh.cn/article44/ddoghe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站制作、ChatGPT、靜態(tài)網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容