Creating Unicode character from its number

后端 未结 13 1806
挽巷
挽巷 2020-11-28 21:38

I want to display a Unicode character in Java. If I do this, it works just fine:

String symbol = \"\\u2202\";

symbol is equal to \"∂\". That\'

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 22:06

    The other answers here either only support unicode up to U+FFFF (the answers dealing with just one instance of char) or don't tell how to get to the actual symbol (the answers stopping at Character.toChars() or using incorrect method after that), so adding my answer here, too.

    To support supplementary code points also, this is what needs to be done:

    // this character:
    // http://www.isthisthingon.org/unicode/index.php?page=1F&subpage=4&glyph=1F495
    // using code points here, not U+n notation
    // for equivalence with U+n, below would be 0xnnnn
    int codePoint = 128149;
    // converting to char[] pair
    char[] charPair = Character.toChars(codePoint);
    // and to String, containing the character we want
    String symbol = new String(charPair);
    
    // we now have str with the desired character as the first item
    // confirm that we indeed have character with code point 128149
    System.out.println("First code point: " + symbol.codePointAt(0));
    

    I also did a quick test as to which conversion methods work and which don't

    int codePoint = 128149;
    char[] charPair = Character.toChars(codePoint);
    
    String str = new String(charPair, 0, 2);
    System.out.println("First code point: " + str.codePointAt(0));    // 128149, worked
    String str2 = charPair.toString();
    System.out.println("Second code point: " + str2.codePointAt(0));  // 91, didn't work
    String str3 = new String(charPair);
    System.out.println("Third code point: " + str3.codePointAt(0));   // 128149, worked
    String str4 = String.valueOf(codePoint);
    System.out.println("Fourth code point: " + str4.codePointAt(0));  // 49, didn't work
    String str5 = new String(new int[] {codePoint}, 0, 1);
    System.out.println("Fifth code point: " + str5.codePointAt(0));   // 128149, worked
    

提交回复
热议问题