Java用表情替换文本

huangapple go评论89阅读模式
英文:

Java replace text with emoji

问题

以下是翻译好的部分:

我制作了这个类,可以将文本替换为表情符号。目前代码运行正常,但表情符号的外观不太正确。

它们是这样的:
Java用表情替换文本

但我更希望它们是这样的:
Java用表情替换文本

我尝试了多种不同的代码格式,但似乎没有任何效果。也许我在搜索错误的内容?

我的类:

package user;

import java.util.HashMap;
import java.util.Map;

public class EmojiReplacer {

    private final Map<String, Integer> emojis = new HashMap<String, Integer>() {{
        put(":)", 0x1F60A);
        put(":d", 0x1F604);
        put(";)", 0x1F609);
        put(":o", 0x1F62E);
        put(":p", 0x1F60B);
        put(":$", 0x1F633);
        put(":(", 0x1F615);
        put(":&#39;(", 0x1F625);
        put(":|", 0x1F610);
        put(">:)", 0x1F608);
        put(">:(", 0x1F621);
        put(":]", 0x1F60F);
        put("<3", 0x2764);
    }};

    public String replaceString(String message) {
        String[] messagePieces = message.split("\\s+");
        for (int i = 0; i < messagePieces.length; i++) {
            if (emojis.containsKey(messagePieces[i])) {
                messagePieces[i] = new String(Character.toChars(emojis.get(messagePieces[i])));
            }
        }
        return String.join(" ", messagePieces);
    }
}
英文:

I made this class that will replace text to emoji. The code works fine at the moment but the look of the emoji isn't quite right.

They are like this:
Java用表情替换文本

But I rather have them like this:
Java用表情替换文本

I tried multiple different code formats, but nothing seems to work. Maybe I am searching on the wrong thing?

My class:

package user;

import java.util.HashMap;
import java.util.Map;

public class EmojiReplacer {

    private final Map&lt;String, Integer&gt; emojis = new HashMap&lt;String, Integer&gt;() {{
        put(&quot;:)&quot;, 0x1F60A);
        put(&quot;:d&quot;, 0x1F604);
        put(&quot;;)&quot;, 0x1F609);
        put(&quot;:o&quot;, 0x1F62E);
        put(&quot;:p&quot;, 0x1F60B);
        put(&quot;:$&quot;, 0x1F633);
        put(&quot;:(&quot;, 0x1F615);
        put(&quot;:&#39;(&quot;, 0x1F625);
        put(&quot;:|&quot;, 0x1F610);
        put(&quot;&gt;:)&quot;, 0x1F608);
        put(&quot;&gt;:(&quot;, 0x1F621);
        put(&quot;:]&quot;, 0x1F60F);
        put(&quot;&lt;3&quot;, 0x2764);
    }};

    public String replaceString(String message) {
        String[] messagePieces = message.split(&quot;\\s+&quot;);
        for (int i = 0; i &lt; messagePieces.length; i++) {
            if (emojis.containsKey(messagePieces[i])) {
                messagePieces[i] = new String(Character.toChars(emojis.get(messagePieces[i])));
            }
        }
        return String.join(&quot; &quot;, messagePieces);
    }
}

答案1

得分: 1

我的猜测是问题出在渲染表情符号的部分,而不是您要渲染的文本字符串。换句话说,您的代码运行正常。

我刚刚像这样运行了您的代码:

public static void main(String[] args) {
    EmojiReplacer test = new EmojiReplacer();
    System.out.println(test.replaceString(":) :d >:) :$"));
}

我得到了这个结果:

&#128522; &#128516; &#128520; &#128563;

这在我的IntelliJ输出窗口和这里都看起来很好。我使用的是Mac。

是什么将您的代码生成的字符串转化为实际的视觉表情符号呢?由于浏览器显然可以渲染它们,您是否尝试将您认为看起来不正确的表情符号复制并粘贴到浏览器中,例如粘贴到您的消息中。您展示的糟糕表情符号的示例似乎是一个屏幕截图,而不是浏览器渲染的实际表情符号。

英文:

My guess is that the problem is whatever is rendering the emoji for you, not the string of text that you are rendering. In other words, your code is working fine.

I just ran your code like this:

public static void main(String[] args) {
    EmojiReplacer test = new EmojiReplacer();
    System.out.println(test.replaceString(&quot;:) :d &gt;:) :$&quot;));
}

and I get this:

&#128522; &#128516; &#128520; &#128563;

which looked good both in my IntelliJ output window, and here. I'm on a Mac.

What is taking the String that your code produces, and producing the actual visual emojis for you? Since the browser can obviously render them, have you tried copying the emojis that you think look wrong, and pasting them into your browser, like into your message. The example of a bad emoji you show seems to be a screen shot, not the actual emoji rendered by the browser.

huangapple
  • 本文由 发表于 2020年9月28日 03:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定