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


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

Character 类的 getDirectionality(char ch) 方法用于返回给定字符的 unicode 方向性属性。通常,字符方向性计算文本的视觉顺序。 undefined char 值的方向性值为 DIRECTIONALITY_UNDEFINED。

用法

public static byte getDirectionality(char ch)

参数

ch:它是请求方向性属性的字符。

返回值

它返回 char 值的方向性属性。

例子1

public class JavaCharactergetDirectionalityExample1 {
   public static void main(String[] args) {
   // Create two character primitives obj1, obj2
   char obj1, obj2;
   // Assign the values to ch1, ch2
   obj1 = 'Q';
   obj2 = '1';
   // create 2 byte primitives b1, b2
   byte b1, b2;
   // Assign the directionality of obj1 and obj2 to b1 and b2 respectively.
   b1 = Character.getDirectionality(obj1);
   b2 = Character.getDirectionality(obj2);
   System.err.println("The directionality of " + obj1 + " is " + b1);
   System.err.println("The directionality of " + obj2 + " is " + b2);
        }
     }

输出:

The directionality of Q is 0
The directionality of 1 is 3

例子2

import java.util.Scanner;
public class JavaCharactergetDirectionalityExample2 {	
     public static void main(String[] args) {
     // Ask the user for the first input.
       System.out.print("Enter the first input:");
    // Use scanner to get the user input
       Scanner s1 = new Scanner(System.in);
    // Gets the first user input
       char[] value1 = s1.nextLine().toCharArray();
    // The Unicode directionality property for the given character
        for (char ch1:value1) {
       byte result1 = Character.getDirectionality(ch1);	
System.out.println("The Unicode directionality property for the character " + ch1+ " is " + result1);		
    }
    // Ask the user for the sercond input.
          System.out.print("Enter the second input:");
    // use scanner to get the user input
          Scanner s2 = new Scanner(System.in);
    // gets the user input
       char[] value2 = s2.nextLine().toCharArray();
    // The Unicode directionality property for the given character
       for (char ch2:value2) {
       byte result2 = Character.getDirectionality(ch2);
System.out.println("The Unicode directionality property for the character " + ch2+ " is " + result2);		
        }	
       } 
}

输出:

Enter the first input:D
The Unicode directionality property for the character D is 0
Enter the second input:4
The Unicode directionality property for the character 4 is 3

例子3

public class JavaCharactergetDirectionalityExample3 {
    public static void main(String[] args)
      {
      // Create two int primitives ch1 and ch2 and assign values to them
           int char1 = 45, char2 = 61;
     // Assign the directionality
           byte obj1 = Character.getDirectionality(char1);
           byte obj2 = Character.getDirectionality(char2);
           System.err.println("Directionality of first primitive is " + obj1);
           System.err.println("Directionality of second primitive is " + obj2);
        }
    }

输出:

Directionality of first primitive is 4
Directionality of second primitive is 13

Java 字符 getDirectionality() 方法

Character 类的 getDirectionality(int codePoint) 方法用于返回给定字符的 unicode 方向性属性。通常,字符方向性用于计算文本的视觉顺序。未定义字符值的方向性值为 DIRECTIONALITY_UNDEFINED。

用法

public static byte getDirectionality(int codePoint)

参数

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

codePoint: 它是请求方向性属性的字符(Unicode 代码点)。

返回值

getDirectionality(int codePoint) 方法返回字符的方向性属性。

例子1

public class JavaCharactergetDirectionality_Example1 {
     public static void main(String[] args) {
    // Initialize the codePoints.
        int codepoint1 = 36;
        int codepoint2 = 2001;
    // Convert the codePoint to its numeric value.
         byte value1 = Character.getDirectionality(codepoint1);
         byte value2 = Character.getDirectionality(codepoint2);
       // Print the result.
        System.out.println("codePoint:" + codepoint1);
           System.out.println("Unicode directionality property for the first codePoint is:"+ value1);
        System.out.println("codePoint:" + codepoint1);
        System.out.println("Unicode directionality property for the second codePoint is:" + value2);		
        }
    }

输出:

codePoint:36
Unicode directionality property for the first codePoint is:5
codePoint:36
Unicode directionality property for the second codePoint is:1

例子2

public class JavaCharactergetDirectionality_Example2 {
   public static void main(String[] args) {
    // Create two int primitives cp1, cp2
          int obj1, obj2;
      // Assign the values to obj1, obj2
          obj1 = 323;
          obj2 = 60;
     // create two byte primitives b1, b2
          byte b1, b2;
      // assign directionality of cp1, cp2 to b1, b2
          b1 = Character.getDirectionality(obj1);
          b2 = Character.getDirectionality(obj2);	       
          System.err.println("Directionality for first codePoint is " + b1);
          System.err.println("Directionality of second codePoint is " + b2); 
       }
    }

输出:

Directionality for first codePoint is 0
Directionality of second codePoint is 13

例子3

public class JavaCharactergetDirectionality_Example3 {	
    public static void main(String[] args) {
    // Initialize the codePoints.
       int codepoint1 = 36;
          int codepoint2 = 2001;
          int codepoint3 = 00;
    // Convert the codePoint to its numeric value.
       byte value1 = Character.getDirectionality(codepoint1);
       byte value2 = Character.getDirectionality(codepoint2);
          byte value3 = Character.getDirectionality(codepoint3); 
       // Print the result.
    System.out.println("codePoint:" + codepoint1);      
  System.out.println("Unicode directionality property for the first codePoint is:"+ value1);
      System.out.println("codePoint:" + codepoint2);	 
   System.out.println("Unicode directionality property for the second codePoint is:" + value2);	
      System.out.println("codePoint:" + codepoint3);	
    System.out.println("Unicode directionality property for the second codePoint is:" + value3);		
             }
     }

输出:

codePoint:36
Unicode directionality property for the first codePoint is:5
codePoint:2001
Unicode directionality property for the second codePoint is:1
codePoint:0
Unicode directionality property for the second codePoint is:9



相关用法


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