當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character isDigit()用法及代碼示例


  1. java.lang.Character.isDigit(char ch)是java中的一種內置方法,該方法確定指定字符是否為數字。字符必須滿足幾個條件才能被接受為數字。也就是說,如果Character.getType(ch)提供的字符的常規類別類型為DECIMAL_DIGIT_NUMBER,則該字符為數字。某些包含數字的Unicode字符範圍:

    From ‘\u0030’ to ‘\u0039’:ISO-LATIN-1 digits (‘0’ through ‘9’)
    From ‘\u0660’ to ‘\u0669’:Arabic-Indic digits
    From ‘\u06F0’ to ‘\u06F9’:Extended Arabic-Indic digits
    From ‘\u0966’ to ‘\u096F’:Devanagari digits
    From ‘\uFF10’ to ‘\uFF19’:Fullwidth digits

    除上述範圍外,許多其他字符範圍也包含數字。



    用法:

    public static boolean isDigit(char ch)
    

    參數:此方法接受字符參數ch作為參數,將對其進行測試。

    返回值:此方法返回一個布爾值。如果ch是數字,則返回True,否則返回False。

    注意:此方法不能處理補充字符。要支持所有Unicode字符(包括補碼),請使用isDigit(int)方法。

    以下示例程序旨在說明上述方法:
    程序1:

    // Java program to illustrate the 
    // Character.isDigit() method 
      
    import java.util.*; 
    import java.lang.*; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
      
            // two characters 
            char c1 = 'A', c2 = '4'; 
      
            // Function to check if the character 
            // is digit or not 
            System.out.println( 
                c1 + " is a digit -> "
                + Character.isDigit(c1)); 
            System.out.println( 
                c2 + " is a digit -> "
                + Character.isDigit(c2)); 
        } 
    }
    輸出:
    A is a digit -> false
    4 is a digit -> true
    

    程序2:

    // Java program to illustrate the 
    // Character.isDigit() method 
      
    import java.util.*; 
    import java.lang.*; 
      
    public class GFG { 
      
        public static int search_digit(String s) 
        { 
      
            // Function to check if is digit 
            // is found or not 
            for (int i = 0; i < s.length(); i++) { 
                if (Character.isDigit( 
                        s.charAt(i)) 
                    == true) { 
                    // return position of digit 
                    return i + 1; 
                } 
            } 
            // return 0 if digit not present 
            return 0; 
        } 
      
        public static void main(String[] args) 
        { 
      
            // Array of strings 
            String[] arr = { "ABC4DEF", "QWERTY" }; 
      
            // To store the position of digit 
            int index = 0; 
      
            // Traverse the array arr[] to find digit 
            // within it's elements 
            for (String x:arr) { 
                index = search_digit(x); 
                if (index != 0) { 
                    System.out.println( 
                        "Digit found at:"
                        + (index) 
                        + "th position."); 
                } 
                else { 
                    System.out.println( 
                        "Digit not present."); 
                } 
            } 
        } 
    }
    輸出:
    Digit found at:4th position.
    Digit not present.
    
  2. java.lang.Character.isDigit(int codePoint)是java中的一個內置方法,它確定整數類型的指定Unicode代碼點字符是否為數字。字符必須滿足幾個條件才能被接受為數字。也就是說,如果由getType(codepoint)提供的字符的常規類別類型為DECIMAL_DIGIT_NUMBER,則該字符為數字。某些包含數字的Unicode字符範圍:

    From ‘\u0030’ to ‘\u0039’:ISO-LATIN-1 digits (‘0’ through ‘9’)
    From ‘\u0660’ to ‘\u0669’:Arabic-Indic digits
    From ‘\u06F0’ to ‘\u06F9’:Extended Arabic-Indic digits
    From ‘\u0966’ to ‘\u096F’:Devanagari digits
    From ‘\uFF10’ to ‘\uFF19’:Fullwidth digits



    除上述範圍外,許多其他字符範圍也包含數字。

    用法:

    public static boolean isDigit(int codePoint)
    

    參數:此方法接受整數類型的Unicode字符參數codePoint作為參數,將對其進行測試。

    返回值:此方法返回一個布爾值。如果指定字符為數字,則返回True,否則返回False。

    以下示例程序旨在說明上述方法:
    程序1:

    // This program demonstrates the use of 
    // isDigit(int codePoint) method of Character class. 
    import java.util.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
            // create codePoints 
            int cp1 = 57; 
            int cp2 = 84; 
      
            // Check whether the codePoints 
            // are digit or not. 
            System.out.println( 
                "The codePoint cp1 is a digit -> "
                + Character.isDigit(cp1)); 
            System.out.println( 
                "The codePoint cp2 is a digit -> "
                + Character.isDigit(cp2)); 
        } 
    }
    輸出:
    The codePoint cp1 is a digit -> true
    The codePoint cp2 is a digit -> false
    

    程序2:

    // This program demonstrates the use of 
    // isDigit(int codePoint) method of 
    // Character class. 
    import java.util.*; 
      
    public class Main { 
        public static void main(String[] args) 
        { 
            // create codePoints 
            int cp1 = 0x50; 
            int cp2 = 0x06f8; 
      
            // Check whether the codePoints 
            // are digit or not. 
            System.out.println( 
                "The codePoint cp1 is a digit -> "
                + Character.isDigit(cp1)); 
            System.out.println( 
                "The codePoint cp2 is a digit -> "
                + Character.isDigit(cp2)); 
        } 
    }
    輸出:
    The codePoint cp1 is a digit -> false
    The codePoint cp2 is a digit -> true
    

    參考:https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-char-




相關用法


注:本文由純淨天空篩選整理自ashmitraj089大神的英文原創作品 Character isDigit() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。