简体   繁体   English

如何将表示表情符号的unicode字符替换为冒号分隔的字符串表情符号?

[英]How do I replace a unicode Character representing an emoji into a colon delimited String emoji?

I've got a JSON mapping all of the unicode emojis to a colon separated string representation of them (like twitter uses). 我有一个JSON映射将所有unicode表情符号映射到它们的冒号分隔字符串表示(如twitter使用)。 I've imported the file into an ArrayList of Pair< Character, String> and now need to scan a String message and replace any unicode emojis with their string equivalents. 我已经将文件导入到Pair <Character,String>的ArrayList中,现在需要扫描一个String消息,并将任何unicode emojis替换为它们的字符串等价物。

My code for conversion is the following: 我的转换代码如下:

  public static String getStringFromUnicode(Context context, String m) {
    ArrayList<Pair<Character, String>> list = loadEmojis(context);
    String formattedString="";
    for (Pair p : list) {
       formattedString  = message.replaceAll(String.valueOf(p.first), ":" + p.second + ":");
    }
    return formattedString;
}

but I always get the unicode emoji representation when I send the message to a server. 但是当我将消息发送到服务器时,我总是得到unicode表情符号表示。

Any help would be greatly appreciated, thanks!! 任何帮助将不胜感激,谢谢!

When in doubt go back to first principles. 如果有疑问,请回到第一原则。

You have a lot of stuff that is all nested together. 你有很多东西都嵌套在一起。 I have found in such cases that your best approach to solving the problem is to pull it apart and look at what the different pieces are doing. 在这种情况下,我发现解决问题的最佳方法是将其分开并查看不同部分正在做什么。 This lets you take control of the problem, and place test code where needed to see what the data is doing. 这使您可以控制问题,并将测试代码放在需要的位置以查看数据正在执行的操作。

My best guess is that replaceAll() is acting unpredictably; 我最好的猜测是replaceAll()的表现不可预测; misinterpreting the emoji string as commands for its regular expression analysis. 将表情符号字符串误解为其正则表达式分析的命令。

I would suggest substituting replaceAll() with a loop of your own that does the same thing. 我建议用你自己的循环替换replaceAll()做同样的事情。 Since we are working with Unicode I would suggest going down deep on this one. 由于我们正在使用Unicode,我建议深入研究这个问题。 This little code sample will do the same thing as replace all, but because I am addressing the string on a character by character basis it should work no matter what funny controls codes are in the string. 这个小代码示例将执行与替换all相同的操作,但因为我在逐个字符的基础上解决字符串,所以无论字符串中有什么有趣的控件代码,它都应该工作。

String message = "This :-) is a test :-) message";
String find = ":-)";
String replace = "!";
int pos = 0;

//Replicates function of replaceAll without the regular expression analysis
pos = subPos(message,find);
while (pos != -1)
{
   String tmp = message.substring(0,pos);
   tmp = tmp + replace;
   tmp = tmp + message.substring(pos+find.length());
   message = tmp;
   pos = subPos(message,find);
 }
System.out.println(message);


-- Snip --

//Replicates function of indexOf
public static int subPos(String str, String sub)
{
   for (int i = 0; i < str.length() - (sub.length() - 1); i++)
   {
      int j;
      for (j = 0; j < sub.length(); j++)
      {
         System.out.println(i + j);
         if (str.charAt(i + j) != sub.charAt(j))
            break;
      }
      if (j == sub.length()) 
         return i;
   }
   return -1;
}

I hope this helps. 我希望这有帮助。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM