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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。