📜  Java中的字符isDigit() 方法及示例

📅  最后修改于: 2022-05-13 01:55:01.133000             🧑  作者: Mango

Java中的字符isDigit() 方法及示例

  1. Java.lang。 字符.isDigit(char ch)是Java中的内置方法,用于确定指定字符是否为数字。字符必须满足几个条件才能被接受为数字。也就是说,如果字符 .getType(ch) 提供的字符的一般类别类型是 DECIMAL_DIGIT_NUMBER,则该字符是数字。
    一些包含数字的 Unicode字符范围:

    除了上述范围之外,许多其他字符范围也包含数字。

    句法:

    public static boolean isDigit(char ch)
    

    参数:此方法接受字符参数ch作为参数,待测试。

    返回值:该方法返回一个布尔值。如果ch是数字,则返回True ,否则返回False。

    注意:此方法不能处理增补字符。要支持所有 Unicode字符,包括补充字符,请使用 isDigit(int) 方法。

    下面的程序说明了上述方法:
    方案一:

    // 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
    

    方案二:

    // 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。 字符.isDigit(int codePoint)是Java中的一个内置方法,用于确定指定的整数类型的 Unicode 代码点字符是否为数字。
    字符必须满足几个条件才能被接受为数字。也就是说,如果 getType(codepoint) 提供的字符的一般类别类型是 DECIMAL_DIGIT_NUMBER,则该字符是数字。一些包含数字的 Unicode字符范围:

    除了上述范围之外,许多其他字符范围也包含数字。

    句法:

    public static boolean isDigit(int codePoint)
    

    参数:该方法接受整数类型的unicode字符参数codePoint作为参数,待测试。

    返回值:该方法返回一个布尔值。如果指定的字符是数字,则返回True ,否则返回False。

    下面的程序说明了上述方法:
    方案一:

    // 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
    

    方案二:

    // 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: Java/lang/ 字符.html#isDigit-char-