Java - Character isDigit() 方法



Java 的 Character isDigit() 方法用於檢查指定的字元是否為數字。

如果字元的通用類別型別(透過 Character.getType() 方法的返回值獲取)為 DECIMAL_DIGIT_NUMBER,則該字元被認為是數字。

包含數字的一些 Unicode 字元範圍 -

  • '\u0030' 到 '\u0039',ISO-LATIN-1 數字('0' 到 '9')

  • '\u0660' 到 '\u0669',阿拉伯-印度數字

  • '\u06F0' 到 '\u06F9',擴充套件阿拉伯-印度數字

  • '\u0966' 到 '\u096F',梵文數字

  • '\uFF10' 到 '\uFF19',全形數字

許多其他字元範圍也包含數字。

語法

以下是 Java Character isDigit() 方法的語法

public static boolean isDigit(char ch)
(or)
public static boolean isDigit(int codePoint)

引數

  • ch - 要測試的字元

  • codePoint - 要測試的 Unicode 程式碼點

返回值

如果字元是數字,則此方法返回布林值 true,否則返回 false。

檢查 Unicode 中定義的字元示例

以下示例演示了 Java Character isDigit() 方法的使用。我們建立了 char 變數併為其分配了 char 值。現在使用 isDigit() 方法,我們檢查 char 值是否為數字。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = '9';
      ch2 = 'V';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isDigit results of ch1, ch2 to b1, b2
      b1 = Character.isDigit(ch1);
      b2 = Character.isDigit(ch2);
      String str1 = ch1 + " is a digit is " + b1;
      String str2 = ch2 + " is a digit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

9 is a digit is true
V is a digit is false

檢查 Unicode 中定義的程式碼點示例

以下示例演示了 Java Character isDigit() 方法的使用。我們建立了 int 變數併為其分配了程式碼點值。現在使用 isDigit() 方法,我們檢查程式碼點值是否為數字。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x06f8;
      cp2 = 0x0c12;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isDigit results of ch1, ch2 to i1, i2
      b1 = Character.isDigit(cp1);
      b2 = Character.isDigit(cp2);
      String str1 = "cp1 represents a digit is " + b1;
      String str2 = "cp2 represents a digit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

cp1 represents a digit is true
cp2 represents a digit is false

檢查 Unicode 中定義的字元示例

以下示例演示了 Java Character isDigit() 方法的使用。我們建立了一個 char 變數併為其分配了一個 char 值。現在使用 isDigit() 方法,我們檢查 char 值是否為數字。

由於此方法返回布林型別的值,因此我們可以將其與條件語句一起使用。下面顯示了一個示例。

package com.tutorialspoint;

public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('5');
      if(Character.isDigit(c1))
         System.out.println("The input character is a digit");
      else
         System.out.println("The input character is not a digit");
   }
}

輸出

編譯並執行上面的程式碼後,我們將得到如下所示的輸出 -

The input character is a digit

檢查 Unicode 中定義的字元示例

以下示例演示了 Java Character isDigit() 方法的使用。我們建立了一個 Character 變數併為其分配了一個 Character 物件。現在使用 isDigit() 方法,我們檢查 Character 值是否為數字。

另一個在此方法上使用條件語句的示例如下 -

package com.tutorialspoint;

public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('a');
      if(Character.isDigit(c1))
         System.out.println("The input character is a digit");
      else
         System.out.println("The input character is not a digit");
   }
}

輸出

編譯並執行上面的程式碼後,我們將得到如下所示的輸出 -

The input character is not a digit
java_lang_character.htm
廣告