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


Java Character.isLetterOrDigit()用法及代码示例


java.lang.Character.isLetterOrDigit(char ch)是java中的一种内置方法,该方法确定指定字符是字母还是数字。

用法:

public static boolean isLetterOrDigit(char ch)

参数:该函数接受单个强制参数ch,该参数表示要测试的字符。


返回值:该函数返回一个布尔值。如果字符是字母或数字,则布尔值为true,否则为false。

以下示例程序旨在说明上述方法:

示例1:

// Java program to illustrate the 
// Character.isLetterOrDigit() method 
  
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // two characters 
        char c1 = 'Z', c2 = '2'; 
  
        // Function to check if the character is letter or digit 
        boolean bool1 = Character.isLetterOrDigit(c1); 
        System.out.println(c1 + " is a letter/digit ? " + bool1); 
  
        // Function to check if the character is letter or digit 
        boolean bool2 = Character.isLetterOrDigit(c2); 
        System.out.println(c2 + " is a letter/digit ? " + bool2); 
    } 
}
输出:
Z is a letter/digit ? true
2 is a letter/digit ? true

示例2:

// Java program to illustrate the 
// Character.isLetterOrDigit() method 
  
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // assign character 
        char c1 = 'D', c2 = '/'; 
  
        // Function to check if the character is letter or digit 
        boolean bool1 = Character.isLetterOrDigit(c1); 
        System.out.println(c1 + " is a letter/digit ? " + bool1); 
  
        // Function to check if the character is letter or digit 
        boolean bool2 = Character.isLetterOrDigit(c2); 
        System.out.println(c2 + " is a letter/digit ? " + bool2); 
    } 
}
输出:
D is a letter/digit ? true
/ is a letter/digit ? false

参考:https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char)



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Character.isLetterOrDigit() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。