Java - Character getDirectionality() Method



The Java Character getDirectionality() method will retrieve the Unicode directionality property for the given character.

Character directionality is used to calculate the visual ordering of text. For example, few languages, like English, are written from left to right; whereas other scripts, like Arabic and Urdu, are written from right to left. However, the Arabic script is not necessarily written from right to left always, the numbers and mathematical dates etc. are written from left to right. In such cases, this method is beneficial to determine the directionality of a character.

Note: The directionality value of undefined char values is DIRECTIONALITY_UNDEFINED.

Syntax

Following is the syntax for Java Character getDirectionality() method

public static byte getDirectionality(char ch)
or,
public static byte getDirectionality(int codePoint)

Parameters

  • ch − char for which the directionality property is requested

  • codePoint − code point for which the directionality property is requested

Return Value

This method returns the directionality property of the char value. The return type is byte.

Example

The following example shows the usage of Java Character getDirectionality() method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

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

      // assign values to ch1, ch2
      ch1 = 'M';
      ch2 = '\u06ff';

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

      // assign directionality of ch1, ch2 to b1, b2
      b1 = Character.getDirectionality(ch1);
      b2 = Character.getDirectionality(ch2);

      /**
      *  byte value 0 represents DIRECTIONALITY_LEFT_TO_RIGHT
      *  byte value 2 represents DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC
      */

      String str1 = "Directionality of " + ch1 + " is " + b1;
      String str2 = "Directionality of ch2 is " + b2;

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

Output

Let us compile and run the above program, this will produce the following result −

Directionality of M is 0 
Directionality of ch2 is 2

Example

In the following example, we will pass symbols as character arguments to the given method.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch1 = '%';
      char ch2 = '&';
      byte b1 = Character.getDirectionality(ch1);
      byte b2 = Character.getDirectionality(ch2);
      System.out.println("Directionality of " + ch1 + " is " + b1);
      System.out.println("Directionality of ch2 is " + b2);
   }
}

Output

The output for the program above after compiling and executing is as follows −

Directionality of % is 5
Directionality of ch2 is 13

Example

The following example shows the usage of Java Character getDirectionality(int codePoint) method with code points as arguments.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      int cp1, cp2;
      cp1 = 0x2323;
      cp2 = 0x2c60;
      byte b1, b2;
      b1 = Character.getDirectionality(cp1);
      b2 = Character.getDirectionality(cp2);
      System.out.println("Directionality of cp1 is " + b1);
      System.out.println("Directionality of cp2 is " + b2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Directionality of cp1 is 13
Directionality of cp2 is 0

Example

Additional example of both polymorphic forms of the method is given in the program below.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      int cp1;
      char ch1;
      cp1 = 0x2323;
      ch1 = 'y';
      byte b1, b2;
      b1 = Character.getDirectionality(cp1);
      b2 = Character.getDirectionality(ch1);
      System.out.println("Directionality of cp1 is " + b1);
      System.out.println("Directionality of cp2 is " + b2);
   }
}

Output

If we compile and run the program above, the output is displayed as −

Directionality of cp1 is 13
Directionality of cp2 is 0
java_lang_character.htm
Advertisements