Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

java - Create string with emoji unicode flag countries

i need to create a String with a country flag unicode emoji..I did this:

StringBuffer sb = new StringBuffer();
sb.append(StringEscapeUtils.unescapeJava("\u1F1EB"));    
sb.append(StringEscapeUtils.unescapeJava("\u1F1F7"));

Expecting one country flag but i havent..How can i get a unicode country flag emoji in String with the unicodes characters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is, that the "uXXXX" notation is for 4 hexadecimal digits, forming a 16 bit char.

You have Unicode code points above the 16 bit range, both U+F1EB and U+1F1F7. This will be represented with two chars, a so called surrogate pair.
You can either use the codepoints to create a string:

int[] codepoints = {0x1F1EB, 0x1F1F7};
String s = new String(codepoints, 0, codepoints.length);

Or use the surrogate pairs, derivable like this:

System.out.print(""");
for (char ch : s.toCharArray()) {
    System.out.printf("\u%04X", (int)ch);
}
System.out.println(""");

Giving

"uD83CuDDEBuD83CuDDF7"

Response to the comment: How to Decode

"uD83CuDDEB" are two surrogate 16 bit chars representing U+1F1EB and "uD83CuDDF7" is the surrogate pair for U+1F1F7.

private static final int CP_REGIONAL_INDICATOR = 0x1F1E7; // A-Z flag codes.

/**
 * Get the flag codes of two (or one) regional indicator symbols.
 * @param s string starting with 1 or 2 regional indicator symbols.
 * @return one or two ASCII letters for the flag, or null.
 */
public static String regionalIndicator(String s) {
    int cp0 = regionalIndicatorCodePoint(s);
    if (cp0 == -1) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    sb.append((char)(cp0 - CP_REGIONAL_INDICATOR + 'A'));
    int n0 = Character.charCount(cp0);
    int cp1 = regionalIndicatorCodePoint(s.substring(n0));
    if (cp1 != -1) {
        sb.append((char)(cp1 - CP_REGIONAL_INDICATOR + 'A'));
    }
    return sb.toString();
}

private static int regionalIndicatorCodePoint(String s) {
    if (s.isEmpty()) {
        return -1;
    }
    int cp0 = s.codePointAt(0);
    return CP_REGIONAL_INDICATOR > cp0 || cp0 >= CP_REGIONAL_INDICATOR + 26 ? -1 : cp0;
}

System.out.println("Flag: " + regionalIndicator("uD83CuDDEBuD83CuDDF7"));
Flag: EQ

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...