📜  Java中的字符.isMirrored() 和示例

📅  最后修改于: 2022-05-13 01:54:35.239000             🧑  作者: Mango

Java中的字符.isMirrored() 和示例

Java.lang。 字符.isMirrored()是Java中的一个内置方法,根据Unicode规范判断字符是否被镜像。在从右到左的文本中显示时,镜像字符应使其字形水平镜像。例如,'\u0028' 左括号在语义上被定义为左括号。这将在从左到右的文本中显示为“(”,但在从右到左的文本中显示为“)”。镜像字符的一些示例是[ ] { } ( )等。

句法:

public static boolean isMirrored(char ch)

Parameters: 
ch - char for which the mirrored property is requested

返回:如果字符被镜像,则此方法返回 true,否则如果字符未镜像或未定义,则返回 false。

下面给出了字符.isMirrored() 方法的说明:

方案一:

// Java program to demonstrate the
// Character.isMirrored() function
// When the character is a valid one.
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Assign values to ch1, ch2, ch3, ch4
        char ch1 = '[';
        char ch2 = '+';
        char ch3 = '}';
        char ch4 = '(';
  
        // Checks if the character is mirrored or not and prints
        boolean b1 = Character.isMirrored(ch1);
        System.out.println(ch1 + " is a mirrored character is " + b1);
  
        boolean b2 = Character.isMirrored(ch2);
        System.out.println(ch2 + " is a mirrored character is " + b2);
  
        boolean b3 = Character.isMirrored(ch1);
        System.out.println(ch3 + " is a mirrored character is " + b3);
  
        boolean b4 = Character.isMirrored(ch2);
        System.out.println(ch4 + " is a mirrored character is " + b4);
    }
}
输出:
[ is a mirrored character is true
+ is a mirrored character is false
} is a mirrored character is true
( is a mirrored character is false

方案二:

// Java program to demonstrate the
// Character.isMirrored() function
// When the character is a invalid one.
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Assign values to ch1, ch2, ch3, ch4
        char ch1 = '4';
        char ch2 = '0';
  
        // Checks if the character is mirrored or not and prints
        boolean b1 = Character.isMirrored(ch1);
        System.out.println(ch1 + " is a mirrored character is " + b1);
  
        boolean b2 = Character.isMirrored(ch2);
        System.out.println(ch2 + " is a mirrored character is " + b2);
    }
}
输出:
4 is a mirrored character is false
0 is a mirrored character is false