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
广告

© . All rights reserved.