冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 2240|回覆: 0

java 判斷字符串是否為數字十進制十六進制

[複製鏈接]

17

主題

0

好友

15

積分

新手上路

Rank: 1

UID
126691
帖子
39
主題
17
精華
0
積分
15
楓幣
852
威望
15
存款
0
贊助金額
0
推廣
0
GP
16
閱讀權限
10
性別
保密
在線時間
7 小時
註冊時間
2016-1-20
最後登入
2023-6-4
發表於 2018-2-13 21:01:48 |顯示全部樓層
package com.regex.first;

/**
* @ClassName: regexTest1
* @Description: java判断字符串是否为数字。
* @author amosli
* @date 2013-6-28 下午11:46:50
* @Email:amosli@infomorrow.com
*/
public class RegexNumberValidate {
        public static void main(String[] args){
                String[] values = new String[]{
                                "10","32768","9999","ati","905Af","ffff"
                };
                for(String value:values){
                        System.out.println("Validating value:\t"+value);
                        if(isOctNumberRex(value)){
                                System.out.println("this is a Octnumber:"+value);
                        }else {
                                System.out.println("this isn't a Octnumber:"+value);
                        }
                        if(isHexNumberRex(value)){
                                System.out.println("this is a Hexnumber:"+value);
                        }else {
                                System.out.println("this isn't Hexnumber:"+value);
                        }
                }
        }
        //十进制
        private static boolean isOctNumber(String str) {
                boolean flag = false;
                for(int i=0,n=str.length();i<n;i++){
                        char c = str.charAt(i);
                        if(c=='0'|c=='1'|c=='2'|c=='3'|c=='4'|c=='5'|c=='6'|c=='7'|c=='8'|c=='9'){
                                flag =true;
                        }
                }
                return flag;
        }
        //十六进制
        private static boolean isHexNumber(String str){
                boolean flag = false;
                for(int i=0;i<str.length();i++){
                        char cc = str.charAt(i);
                        if(cc=='0'||cc=='1'||cc=='2'||cc=='3'||cc=='4'||cc=='5'||cc=='6'||cc=='7'||cc=='8'||cc=='9'||cc=='A'||cc=='B'||cc=='C'||
                                        cc=='D'||cc=='E'||cc=='F'||cc=='a'||cc=='b'||cc=='c'||cc=='c'||cc=='d'||cc=='e'||cc=='f'){
                                flag = true;
                        }
                }
                return flag;
        }
       
        private static boolean isOctNumberRex(String str){
                String validate = "\\d+";
                return str.matches(validate);
        }
        private static boolean isHexNumberRex(String str){
                String validate = "(?i)[0-9a-f]+";
                return str.matches(validate);
        }
}
/*************print***********************/
Validating value:    10
this is a Octnumber:10
this is a Hexnumber:10
Validating value:    32768
this is a Octnumber:32768
this is a Hexnumber:32768
Validating value:    9999
this is a Octnumber:9999
this is a Hexnumber:9999
Validating value:    ati
this isn't a Octnumber:ati
this is a Hexnumber:ati
Validating value:    905Af
this is a Octnumber:905Af
this is a Hexnumber:905Af
Validating value:    ffff
this isn't a Octnumber:ffff

ewfwefwewegegweg gegwgwegwegwgegegwegwwegwgweg
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-3-28 20:27

回頂部