Java - Character isHighSurrogate() 方法



Java 的 Character isHighSurrogate() 方法用於確定給定的 char 值是否為 Unicode 高代理程式碼單元(也稱為前導代理程式碼單元)。

但是什麼是代理? 代理是 Unicode 中的一對字元,它們由系統中兩個特殊範圍內的程式碼點表示。代理包含兩個值:高代理或前導代理,以及尾隨代理或低代理。

這些值本身並不表示字元,而是用於在 UTF-16 編碼中表示補充字元。高代理的編碼範圍從 D80016 到 DBFF16

語法

以下是 Java Character isHighSurrogate() 方法的語法

public static boolean isHighSurrogate(char ch)

引數

  • ch − 要測試的 char 值

返回值

如果 char 值落在高代理的範圍內,則此方法返回 true,否則返回 false。

檢查字元是否為高代理的示例

以下示例演示了 Java Character isHighSurrogate() 方法的用法。在這個示例中,我們建立了兩個 char 變數併為它們賦值了兩個 Unicode 值。現在使用 isHighSurrogate() 方法,我們檢查這些 char 值是否位於高代理範圍內。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = '\u0c7f';
      ch2 = '\ud8b5';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isHighSurrogate results of ch1, ch2 to b1, b2
      b1 = Character.isHighSurrogate(ch1);
      b2 = Character.isHighSurrogate(ch2);
      String str1 = "ch1 is a Unicode high-surrogate code unit is " + b1;
      String str2 = "ch2 is a Unicode high-surrogate code unit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

ch1 is a Unicode high-surrogate code unit is false
ch2 is a Unicode high-surrogate code unit is true

檢查字元是否為高代理的示例

另一個示例程式使用條件語句來確定傳遞的引數是否為高代理,因為此方法的返回型別是布林型,如下所示:

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      char ch1 = '\ud800';
      if(Character.isHighSurrogate(ch1))
         System.out.println("The character is a Unicode high-surrogate code unit");
      else
         System.out.println("The character is not a Unicode high-surrogate code unit");
   }
}

輸出

方法執行後,將獲得並顯示以下輸出:

The character is a Unicode high-surrogate code unit

檢查字元是否為高代理的示例

在下面的示例中,讓我們來看另一個示例,在這個示例中,我們傳遞的引數值不在高代理範圍內

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      char ch1 = '\u0db3';
      if(Character.isHighSurrogate(ch1))
         System.out.println("The character is a Unicode high-surrogate code unit");
      else
         System.out.println("The character is not a Unicode high-surrogate code unit");
   }
}

輸出

方法執行後,將獲得並顯示以下輸出:

The character is not a Unicode high-surrogate code unit

檢查字元陣列中的字元是否為高代理的示例

我們還可以檢查字元陣列中存在的元素是否為高代理。

下面的示例宣告並初始化了一個字元陣列。使用迴圈語句將此陣列中的每個元素作為引數傳遞給此方法。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      char ch[] = new char[] {'\ud833', '\ub754', '\u0611', '\udbff', '\u5372'};
      int count = 0;
      for(int i = 0; i < ch.length; i++){
         if(Character.isHighSurrogate(ch[i])){
            System.out.println("The character is a Unicode high-surrogate code unit");
            count++;
         }
         else
            System.out.println("The character is not a Unicode high-surrogate code unit");
      }
        if(count == 0)
           System.out.println("Unicode high-surrogate code unit does not exist in this array");
   }
}

輸出

編譯並執行上面給出的程式,輸出將如下所示:

The character is a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
The character is a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
java_lang_character.htm
廣告
© . All rights reserved.