Java - Character isMirrored() 方法



描述

Java 的 Character isMirrored() 方法用于确定字符是否根据 Unicode 规范进行镜像。在从右到左阅读的文本中显示时,镜像字符的字形(也称为其设计或表示)应水平镜像。

例如,'\u0028' 左括号是 Unicode 字符的开括号。它在从左到右的文本中将显示为 "(",但在从右到左的文本中将显示为 ")"。

此方法以两种多态形式出现,具有不同的参数类型:char 值和整数代码点值。

语法

以下是 Java Character isMirrored() 方法的语法

public static boolean isMirrored(char ch)
(or)
public static boolean isMirrored(int codePoint)

参数

  • ch - 请求镜像属性的字符

  • codePoint - 请求镜像属性的 Unicode 代码点

返回值

如果字符是镜像的,则此方法返回 true;如果字符不是镜像的或未定义,则返回 false。

检查字符是否镜像的示例

以下示例显示了 Java Character isMirrored(char ch) 方法的用法。我们用两个字符值初始化了两个字符变量。使用 isMirrored() 方法,我们检查字符变量是否包含镜像字符。然后打印结果。

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 = '}';
      ch2 = '^';

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

      // check if ch1, ch2 are mirrored and assign results to b1, b2
      b1 = Character.isMirrored(ch1);
      b2 = Character.isMirrored(ch2);

      String str1 = ch1 + " is a mirrored character is " + b1;
      String str2 = ch2 + " is a mirrored character is " + b2;

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

输出

让我们编译并运行以上程序,这将产生以下结果:

} is a mirrored character is true
^ is a mirrored character is false

检查代码点是否镜像的示例

以下示例显示了 Java Character isMirrored(int codepoint) 方法的用法。我们用两个代码点值初始化了两个整型变量。使用 isMirrored() 方法,我们检查整型变量是否包含镜像字符。然后打印结果。

package com.tutorialspoint;

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

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0c01;
      cp2 = 0x003c; // represents <

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

      /**
       *  check if cp1, cp2 represent mirrored characters
       *  and assign results to b1, b2
       */
      b1 = Character.isMirrored(cp1);
      b2 = Character.isMirrored(cp2);
      String str1 = "cp1 represents a mirrored character is " + b1;
      String str2 = "cp2 represents a mirrored character is " + b2;

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

输出

让我们编译并运行以上程序,这将产生以下结果:

cp1 represents a mirrored character is false
cp2 represents a mirrored character is true

检查字符是否镜像的示例

由于此方法的返回类型为布尔值,因此我们还可以将条件语句与该方法作为其条件一起使用,以检查字符是否为镜像字符。

package com.tutorialspoint;

public class MirroredDemo{
   public static void main(String []args) {
      char c1 = '[';
      char c2 = '{';
      char c3 = '/';
      if(Character.isMirrored(c1)) {
         System.out.println(c1 + " is the mirrored character");
      }
      if(Character.isMirrored(c2)) {
         System.out.println(c2 + " is the mirrored character");
      }
      if(Character.isMirrored(c3)) {
         System.out.println(c3 + " is the mirrored character");
      }
      else {
         System.out.println("Not a mirrored character");
      }
   }
}

输出

编译并运行以上程序以显示以下输出:

[ is the mirrored character
{ is the mirrored character
Not a mirrored character

检查字符是否镜像的示例

条件语句也可以在循环中使用。在这里,我们初始化一个字符数组,并使用循环语句将其元素作为参数传递给方法。每个元素都使用条件语句检查是否为镜像字符。

package com.tutorialspoint;

public class MirroredDemo{
   public static void main(String []args) {
      char c[] = {'(', ']', '-', '{', '<', '|'};
      for(int i = 0; i < c.length; i++) {
         if(Character.isMirrored(c[i])) {
            System.out.println(c[i] + " character is mirrored");
         }
         else {
            System.out.println(c[i] + " character is not mirrored");
         }
      }
   }
}

输出

编译并运行以上程序后,将显示以下输出:

( character is mirrored
] character is mirrored
- character is not mirrored
{ character is mirrored
< character is mirrored
| character is not mirrored
java_lang_character.htm
广告