當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。