首页 > 解决方案 > 如何在java中找到符号的代理对

问题描述

我试图让 and 操作(⋀)符号出现,但符号的 unicode 值为 u+22C1(这可能是错误的,但根据我读过的内容是这样的)。我发现另一个值是 2227,但它会打印 ࢳ。如果你能解释一下找到代理对的方法,因为我必须找到它才能找到更多符号。

标签: javaunicode

解决方案


在 Java 中查找符号的代理对很简单,但有几点需要注意:

  • 并非所有字符都由代理对表示,包括您的示例(“⋀”),因此在尝试获取代理对之前始终检查这一点。
  • 您需要一种可以在源代码和代码生成的任何输出中显示符号的字体。我在 NetBeans 中使用Monospaced作为代码和输出如下所示。

这是显示任意符号的一些基本 Unicode 信息的代码。它获取符号的代码点,确定它是否由代理对表示,如果是,则获取高代理和低代理。该代码处理两个符号,一个带有代理对(表情符号“”),一个没有(您的“⋀”示例)。

package surrogates;

public class Surrogates {

    public static void main(String[] args) {
        Surrogates.displaySymbolDetails("️️");
        Surrogates.displaySymbolDetails("⋀️️");
    }

    static void displaySymbolDetails(String symbol) {
        int cp = symbol.codePointAt(0);
        String name = Character.getName(cp);
        System.out.println(symbol + " has code point " + cp + " (hex " + Integer.toHexString(cp) + ").");
        System.out.println(symbol + " has Unicode name " + name + ".");
        boolean isSupplemenetary = Character.isSupplementaryCodePoint(cp);
        if (isSupplemenetary) {
            System.out.println(symbol + " is a supplementary character.");
            char high = Character.highSurrogate​(cp);
            char low = Character.lowSurrogate​(cp);
            System.out.println(symbol + " has high surrogate: " + (int) high + ".");
            System.out.println(symbol + " has low surrogate: " + (int) low + ".");
        } else {
            System.out.println(symbol + " is in the BMP and therefore is not represented by a surrogate pair.");
        }
    }
}

这是输出:

️️ has code point 128522 (hex 1f60a).
️️ has Unicode name SMILING FACE WITH SMILING EYES.
️️ is a supplementary character.
️️ has high surrogate: 55357.
️️ has low surrogate: 56842.
⋀️️ has code point 8896 (hex 22c0).
⋀️️ has Unicode name N-ARY LOGICAL AND.
⋀️️ is in the BMP and therefore is not represented by a surrogate pair.

笔记:

  • “符号”可以表示多种含义,但我假设在您的问题中您只是指一些 Unicode 字符。
  • 基本多语言平面 (BMP) 中的符号(即 Unicode 字符)不是由代理对表示的。所有其他符号都在某个补充平面 (SMP) 中,并由代理对表示。

推荐阅读