Java 使用表情符号 unicode 标志国家创建字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26231263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:03:09  来源:igfitidea点击:

Create string with emoji unicode flag countries

javaunicodeemoji

提问by colymore

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

我需要创建一个带有国旗 unicode 表情符号的字符串..我这样做了:

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?

期待一个国家国旗,但我还没有……我怎样才能在字符串中获得一个带有 unicodes 字符的 unicode 国家国旗表情符号?

采纳答案by leo

You should be able to do that simply using toCharsfrom java.lang.Character.

您应该能够简单地使用toCharsfrom来做到这一点java.lang.Character

This works for me:

这对我有用:

    StringBuffer sb = new StringBuffer();
    sb.append(Character.toChars(127467));
    sb.append(Character.toChars(127479));
    System.out.println(sb);

prints , which the client can chose todisplay like a french flag, or in other ways.

打印,客户可以选择像法国国旗一样显示,或以其他方式显示

回答by Thomas

Look at Creating Unicode character from its number

查看从其编号创建 Unicode 字符

Could not get my machine to print the Unicode you have there, but for other values it works.

无法让我的机器打印您在那里的 Unicode,但对于其他值,它可以工作。

回答by Joop Eggen

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

问题是,“\uXXXX”表示法用于 4 个十六进制数字,形成 16 位char.

You have Unicode code points abovethe 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:

Unicode 代码点高于16 位范围,包括 U+F1EB 和 U+1F1F7。这将用两个字符表示,即所谓的代理对。
您可以使用代码点来创建字符串:

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

给予

"\uD83C\uDDEB\uD83C\uDDF7"


Response to the comment: How to Decode

回复评论:如何解码

"\uD83C\uDDEB" are two surrogate 16 bit chars representing U+1F1EB and "\uD83C\uDDF7" is the surrogate pair for U+1F1F7.

"\uD83C\uDDEB" 是代表 U+1F1EB 的两个代理 16 位字符,"\uD83C\uDDF7" 是 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("\uD83C\uDDEB\uD83C\uDDF7"));
Flag: EQ

回答by Vincent Durmont

If you want to use emojis often, it could be good to use a library that would handle that unicode stuff for you: emoji-java

如果您想经常使用表情符号,最好使用一个可以为您处理 unicode 内容的库:emoji-java

You would just add the maven dependency:

您只需添加 maven 依赖项:

<dependency>
  <groupId>com.vdurmont</groupId>
  <artifactId>emoji-java</artifactId>
  <version>1.0.0</version>
</dependency>

And call the EmojiManager:

并调用 EmojiManager:

Emoji emoji = EmojiManager.getForAlias("fr");
System.out.println("HEY: " + emoji.getUnicode());

The entire list of supported emojis is here.

支持的表情符号的完整列表在这里

回答by Cristian Babarusi

I suppose you want to achieve something like this

我想你想实现这样的目标

enter image description here

在此处输入图片说明

Let me give you 2 example of unicodes for country flags:

让我给你 2 个国家标志的 unicode 示例:

for ROMANIA ---> \uD83C\uDDF7\uD83C\uDDF4

罗马尼亚 ---> \uD83C\uDDF7\uD83C\uDDF4

for AMERICA ---> \uD83C\uDDFA\uD83C\uDDF8

对于美国 ---> \uD83C\uDDFA\uD83C\uDDF8

You can get this and other country flags unicodes from this site Emoji Unicodes

你可以从这个网站Emoji Unicodes获得这个和其他国家的国旗unicodes

Once you enter the site, you will see a table with a lot of emoji. Select the tab with FLAGS from that table (is easy to find it) then will appear all the country flags. You need to select one flag from the list, any flag you want... but only ONE. After that will appear a text code in the message box...that is not important. Important is that you have to look in the right of the site where will appear flag and country name of your selected flag. CLICK on that, and on the page that will open you need to find the TABLE named Emoji Character Encoding Data. Scroll until the last part of table where sais: C/C++/Java Src .. there you will find the correct unicode flag. Attention, always select the unicode that is long like that, some times if you are not carefull you can select a simple unicode, not long like that. So, keep that in mind.

进入网站后,您会看到一张带有很多表情符号的表格。从该表中选择带有 FLAGS 的选项卡(很容易找到),然后将出现所有国家/地区的国旗。您需要从列表中选择一个标志,您想要的任何标志......但只有一个。之后会在消息框中出现一个文本代码......这并不重要。重要的是,您必须在站点右侧查看您选择的旗帜的旗帜和国家名称。单击那个,在打开的页面上,您需要找到名为 Emoji Character Encoding Data 的 TABLE。滚动到表格的最后一部分,其中 sais: C/C++/Java Src .. 在那里你会找到正确的 unicode 标志。注意,总是选择那么长的unicode,有时候不小心可以选择一个简单的unicode,不那么长。所以记住这一点。

Indications image 1

适应症图像 1

enter image description here

在此处输入图片说明

Indication image 2

表示图像2

enter image description here

在此处输入图片说明

In the end i will post a sample code from an Android app of mine that will work on java the same way.

最后,我将发布一个来自我的 Android 应用程序的示例代码,它可以以相同的方式在 Java 上运行。

ArrayList<String> listLanguages = new ArrayList<>();
    listLanguages.add("\uD83C\uDDFA\uD83C\uDDF8  " + getString(R.string.English));
    listLanguages.add("\uD83C\uDDF7\uD83C\uDDF4  " + getString(R.string.Romanian));

Another simple custom example:

另一个简单的自定义示例:

String flagCountryName = "\uD83C\uDDEF\uD83C\uDDF2 Jamaica";

You can use this variable where you need it. This will show you the flag of Jamaica in front of the text.

您可以在需要的地方使用此变量。这将在文本前显示牙买加国旗。

This is all, if you did not understand something just ask.

这就是全部,如果你不明白什么就问。