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


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