Java判斷字符是否是數(shù)字的方法-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)Java判斷字符是否是數(shù)字的方法,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、鹽池網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

一、使用自帶函數(shù)isDigit()

語法:

public static boolean isDigit(char ch)

實例:

public static boolean isNumeric (String str) {
    for (int i = str.length(); --i >=0) {
          if (!Character.isDigit(str.charAt(i))) {
                return false;
          }
    }
    return true;}

二、使用正則表達(dá)式

實例1:

public static boolean isNumeric(String str) {
    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
    return pattern.matcher(str).matches();}

實例2:

public static boolean isNumeric(String str) {
    if (str != null && !"".equals(str.trim())) {
          return s.matches("^[0-9]*$");
    }
    return false;}

實例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ù)字的方法,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:Java判斷字符是否是數(shù)字的方法-創(chuàng)新互聯(lián)
文章路徑:http://muchs.cn/article44/ddoghe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站制作ChatGPT、靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計