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


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

Character 类的 isDefined(char ch) 方法一般判断该字符是否在 Unicode 中定义。

当且仅当满足以下两个条件中的任何一个(或为真)时,才在 Unicode 中定义字符:

  • 该字符必须在 UnicodeData 文件中有一个条目。
  • 字符必须有一个由 UnicodeData 文件定义的范围。

用法

public static Boolean isDefined(char ch)

参数

上述方法只需要一个参数:

a.) 需要测试的字符。

返回值

如果给定(或指定)字符在 Unicode 中具有定义的含义,则 isDefined(char ch) 方法返回一个布尔值,即 true。否则,它返回 false。

例子1

public class JavaCharacterisDefinedExample1 {
    public static void main(String[] args) {
          // Create char primitives obj1, obj2 and obj3
          char obj1;
          char obj2;
          char obj3;
          // Assign the values to obj1, obj2 and obj3.
          obj1 = '@';
          obj2 = '*';
          obj3 = '&';
          // Create the boolean primitives b1, b2,b3.
          boolean b1;
          boolean b2;
          boolean b3;
          b1 = Character.isDefined(obj1);
          b2 = Character.isDefined(obj2);
          b3 = Character.isDefined(obj3);
          String str1 = "The meaning for the character " +obj1+ " is defined as: " +b1;
          String str2 = "The meaning for the character " +obj2+ " is defined as: " +b2;
          String str3 = "The meaning for the character " +obj3+ " is defined as:  " +b3;
          // print b value
          System.out.println( str1 );
          System.out.println( str2 );
          System.out.println( str3 );
       }
    }

输出:

The meaning for the character @ is defined as: true
The meaning for the character * is defined as: true
The meaning for the character & is defined as: true

例子2

import java.util.Scanner;
public class JavaCharacterisDefinedExample2 {
public static void main(String[] args) {
     // Ask the user to enter the first character.
    System.out.print("Enter a character:  ");		
    // Use scanner to get the user input
    Scanner obj1 = new Scanner(System.in);
   // get a single character
    char value1 = obj1.nextLine().toCharArray()[0];
        
    // Check whether the user input is defined the Unicode or not.
    boolean check1 = Character.isDefined(value1);
   // Print the result
    if(check1){
System.out.print("The user input \'"+value1+"\' is defined in the Unicode\n");
        }
    else{
System.out.print("The user input \'"+value1+"\' is not defined in the Unicode\n");
        }
   // Ask the user to enter the second character.
System.out.print("Enter the second character:  ");				
  // Use scanner to get the user input
    Scanner obj2 = new Scanner(System.in);
  // get a single character
    char value2 = obj2.nextLine().toCharArray()[0];			
  // Check whether the user input is defined the Unicode or not. 
    boolean check2 = Character.isDefined(value2);
  // Print the result
     if(check2){
System.out.print("The user input \'"+value2+"\' is defined in the Unicode");
       }
     else{
System.out.print("The user input \'"+value2+"\' is not defined in the Unicode");
        }		
       }
}

输出:

Enter a character:  $
The user input '$' is defined in the Unicode
Enter the second character:  )
The user input ')' is defined in the Unicode

例子3

public class JavaCharacterisDefinedExample3 {
    public static void main(String args[])
      {
       char ch1 = '1';
       char ch2 = '2';    
       char ch3 = '3';    
       char ch4 = '4';    
       char ch5 = '5';   
       boolean b1 = Character.isDefined(ch1);
       boolean b2 = Character.isDefined(ch2);
       boolean b3 = Character.isDefined(ch3);
       boolean b4 = Character.isDefined(ch4);
       boolean b5 = Character.isDefined(ch5);
System.out.println(ch1 + " is defined in the Unicode character set:" + b1);              
System.out.println(ch2 + " is defined in the Unicode character set:" + b2);  
System.out.println(ch3 + " is defined in the Unicode character set:" + b3);  
System.out.println(ch4 + " is defined in the Unicode character set:" + b4);   
System.out.println(ch5 + " is defined in the Unicode character set:" + b5); 
     	}
}

输出:

1 is defined in the Unicode character set:true
2 is defined in the Unicode character set:true
3 is defined in the Unicode character set:true
4 is defined in the Unicode character set:true
5 is defined in the Unicode character set:true



相关用法


注:本文由纯净天空筛选整理自 Java Character isDefined() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。