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


Java Character isJavaLetterOrDigit()用法及代碼示例


Character 類的 isJavaLetterOrDigit(char ch) 方法確定給定(或指定)字符是否可以作為 Java 標識符的一部分而不是第一個字符。

當且僅當以下任一條件為真時,字符才是 Java 標識符的一部分:

  • 字符是一個字母。
  • 該字符是貨幣符號,如 '$'。
  • 字符是一個數字。
  • 該字符是連接標點符號,如 '_'。
  • 字符是數字字母。
  • 角色是一個組合掩碼。
  • 字符是一個非間距掩碼。
  • isIdentifierIgnorable() 方法為給定的字符返回真。

用法

public static boolean isJavaLetterOrDigit(char ch)

參數

ch: 這是需要測試的字符。

返回值

isJavaLetterOrDigit(char ch) 方法返回一個布爾值,即如果給定(或指定)字符是 Java 標識符的一部分,則返回 true。否則,該方法返回 false。

例子1

public class JavaCharacterisJavaLetterOrDigitExample1 {
    public static void main(String[] args) {
    char ch1 = '3';
    char ch2 = '6';
    char ch3 = '9';

      boolean b1 = Character.isJavaLetterOrDigit(ch1);
      boolean b2 = Character.isJavaLetterOrDigit(ch2);
      boolean b3 = Character.isJavaLetterOrDigit(ch3);

      System.out.println("The result for the first character is given as:"+ b1 );
      System.out.println("The result for the second character is given as:"+ b2 );
      System.out.println("The result for the third character is given as:"+ b3 );
     }
}

輸出:

The result for the first character is given as:true
The result for the second character is given as:true
The result for the third character is given as:true.

例子2

public class JavaCharacterisJavaLetterOrDigitExample2 {
    public static void main(String[] args) {
    char ch1 = 'a';
    char ch2 = 'B';
    char ch3 = 'c';

      boolean b1 = Character.isJavaLetterOrDigit(ch1);
      boolean b2 = Character.isJavaLetterOrDigit(ch2);
      boolean b3 = Character.isJavaLetterOrDigit(ch3);

      System.out.println("The result for the first character is given as:"+ b1 );
      System.out.println("The result for the second character is given as:"+ b2 );
      System.out.println("The result for the third character is given as:"+ b3 );
     }
}

輸出:

The result for the first character is given as:true
The result for the second character is given as:true
The result for the third character is given as:true

例子3

public class JavaCharacterisJavaLetterOrDigitExample3 {
    public static void main(String[] args) {
    char ch1 = 'a';
    char ch2 = '*';
    char ch3 = '5';

      boolean b1 = Character.isJavaLetterOrDigit(ch1);
      boolean b2 = Character.isJavaLetterOrDigit(ch2);
      boolean b3 = Character.isJavaLetterOrDigit(ch3);

      System.out.println("The result for the first character is given as:"+ b1 );
      System.out.println("The result for the second character is given as:"+ b2 );
      System.out.println("The result for the third character is given as:"+ b3 );
     }
}

輸出:

The result for the first character is given as:true
The result for the second character is given as:false
The result for the third character is given as:true




相關用法


注:本文由純淨天空篩選整理自 Java Character isJavaLetterOrDigit() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。