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


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


字符類的 isLowSurrogate(char ch) 方法確定給定(或指定)字符是否為 Unicode 低代理代碼單元(也稱為 trailing-surrogate 代碼單元)。

這些值本身並不代表字符,而是用來代表UTF-26編碼中的增補字符。

用法

public static boolean isLowSurrogate(char ch)

參數

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

返回值

isLowSurrogate(char ch) 方法返回一個布爾值,即如果給定(或指定)字符位於 MIN_LOW_SURROGATE 和 MAX_LOW_SURROGATE 之間,則返回 true。否則,該方法返回 false。

例子1

public class JavaCharacterIsLowSurrogateExample1 {
    public static void main(String[] args) {
      // Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;
      // Assign the values to ch1, ch2 and ch3.
      ch1 = '\udc34';
      ch2 = 'a';
      ch3 = 'Y';
      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
      b1 = Character.isLowSurrogate(ch1);
      b2 = Character.isLowSurrogate(ch2);
      b3 = Character.isLowSurrogate(ch3);

      String str1 = "The first character '"+ch1 + "' is a Unicode low-surrogate:" + b1;
      String str2 = "The second character '"+ch2 + "' is a Unicode low-surrogate:" + b2;
      String str3 = "The third character '"+ch3 + "' is a Unicode low-surrogate:" + b3;
      // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

輸出:

The first character '?' is a Unicode low-surrogate:true
The second character 'a' is a Unicode low-surrogate:false
The third character 'Y' is a Unicode low-surrogate:false

例子2

public class JavaCharacterIsLowSurrogateExample2 {
    public static void main(String[] args) {
      // Create four char primitives ch1, ch2, ch3 and ch4.
      char ch1, ch2, ch3, ch4;
      // Assign the values to ch1, ch2, ch3 and ch4.
      ch1 = '*';
      ch2 = 'a';
      ch3 = 'Y';
      ch4 = '#';
      // Create four boolean primitives b1, b2, b3 and b4.
      boolean b1, b2, b3, b4;
      b1 = Character.isLowSurrogate(ch1);
      b2 = Character.isLowSurrogate(ch2);
      b3 = Character.isLowSurrogate(ch3);
      b4 = Character.isLowSurrogate(ch4);

      String str1 = "The first character '"+ch1 + "'is a Unicode low-surrogate:" + b1;
      String str2 = "The second character '"+ch2 + "' is a Unicode low-surrogate:" + b2;
      String str3 = "The third character '"+ch3 + "' is a Unicode low-surrogate:" + b3;
      String str4 = "The fourth character '"+ch4+ "' is a Unicode low-surrogate:" + b4;   
      //Print the values of b1, b2, b3 and b4.
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
      System.out.println( str4 );
   }
}

輸出:

The first character '*'is a Unicode low-surrogate:false
The second character 'a' is a Unicode low-surrogate:false
The third character 'Y' is a Unicode low-surrogate:false
The fourth character '#' is a Unicode low-surrogate:false

例子3

import java.util.Scanner;
public class JavaCharacterIsLowSurrogateExample3 {
   public static void main(String[] args) {
    // Ask the user for the first input. 
       System.out.print("Enter the first input:");
    // Use the Scanner class to get the user input.
       Scanner s1 = new Scanner(System.in);
    // Gets the user input.
       char[] value1 = s1.nextLine().toCharArray();
    // Check whether the user input is a low surrogate or not.
       for (char ch1:value1) {
   boolean result1 = Character.isLowSurrogate(ch1);
    // print the result
       if(result1){
System.out.println("The character '" + ch1 + "' is a low-surrogate. ");				
                  }
     else{
System.out.println("The character '" + ch1 + "' is not a low-surrogate.");
           }		
       System.out.print("Enter the second input:");
       Scanner s2 = new Scanner(System.in);
       char[] value2 = s2.nextLine().toCharArray();
       for (char ch2:value2) {
      boolean result2 = Character.isLowSurrogate(ch2);
      if(result2){
System.out.println("The character '" + ch2 + "' is a low-surrogate. ");				
                }
      else{
System.out.println("The character '" + ch2 + "' is not a low-surrogate.");
        }		
      System.out.print("Enter the third input:");
       Scanner s3 = new Scanner(System.in);
       char[] value3 = s3.nextLine().toCharArray();
       for (char ch3:value3) {
      boolean result3 = Character.isLowSurrogate(ch3);
      if(result2){
System.out.println("The character '" + ch3 + "' is a low-surrogate. ");				
                }
      else{
System.out.println("The character '" + ch3 + "' is not a low-surrogate..");
           }		
         }
       }
     }
  }
}

輸出:

Enter the first input:H
The character 'H' is not a low-surrogate.
Enter the second input:)
The character ')' is not a low-surrogate.
Enter the third input:6
The character '6' is not a low-surrogate..




相關用法


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