英文:
Java replace text with emoji
问题
以下是翻译好的部分:
我制作了这个类,可以将文本替换为表情符号。目前代码运行正常,但表情符号的外观不太正确。
我尝试了多种不同的代码格式,但似乎没有任何效果。也许我在搜索错误的内容?
我的类:
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(":'(", 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.
But I rather have them like this:
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<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(":'(", 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);
}
}
答案1
得分: 1
我的猜测是问题出在渲染表情符号的部分,而不是您要渲染的文本字符串。换句话说,您的代码运行正常。
我刚刚像这样运行了您的代码:
public static void main(String[] args) {
EmojiReplacer test = new EmojiReplacer();
System.out.println(test.replaceString(":) :d >:) :$"));
}
我得到了这个结果:
😊 😄 😈 😳
这在我的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(":) :d >:) :$"));
}
and I get this:
😊 😄 😈 😳
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论