当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。