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

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

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值