检测是否在一个字符串的字符是(采用Android)表情在一、字符串、字符、表情

2023-09-09 21:05:46 作者:旧城笑眸凌乱一生浮沉

就像标题所说。我想找出一个给定的Java字符串包含一个表情。

Like the title says. I want to find out if a given java String contains an emoticon.

我不能使用 Character.Uni codeBlock.of(炭)== Character.Uni codeBlock.EMOTICONS ,因为这需要API级别19

I can't use Character.UnicodeBlock.of(char) == Character.UnicodeBlock.EMOTICONS since that requires API level 19.

我发现这个code为iOS 但它并不是因为它真的适用看起来像Java和Objective-C以不同的方式处理代理对。

I found this code for iOS but it isn't really applicable since it looks like java and objective-c handle surrogate pairs in different manners.

我通过看该单证告诉我说:

The documentations I've looked through tell me that:

A char value, therefore, represents Basic Multilingual Plane (BMP) code points, including the surrogate code points, or code units of the UTF-16 encoding

我不太清楚这意味着什么。这是否仅仅意味着它们还包括BMP点作为他们的第一个号码是多少?

I'm not quite sure what that means. Does that simply mean that they also include the BMP point as their first number?

根据维基百科的表情符号集介于0x1f600和0x1f64f,但我不知道如何检查,如果一个字符是在这个范围内。

According to Wikipedia the emoticon set lies between 0x1f600 and 0x1f64f but I don't know how to check if the char is in that range.

我希望这样的事情会工作,但事实并非如此。

I had hoped that something like this would work but it didn't

if (0x1f600 <= a && a <= 0x1f64f)
{
    Print.d("Unicode", "groovy!");
}

所以,我怎么去呢?

So how do I go about this?

推荐答案

我其实能够使用链接的iOS code创建下面的函数。我没有意识到包含,例如一个字符串,一个表情都会有2的长度所以,你可以检查一个字,其实是一个代理。

I was in fact able to use the linked iOS code to create the following function. I didn't realize that a String that contains, for example, a single emoticon will have a length of 2. So you can check if a character is in fact a surrogate.

我不完全知道如何处理否则,如果(substring.length→1)从iOS code,但我觉得 Character.isHighSurrogate(myChar)确实在该实例同样的工作。

I'm not entirely sure how to handle else if (substring.length > 1) from the iOS code but I think Character.isHighSurrogate(myChar) does the same job in that instance.

private boolean containsIllegalCharacters(String displayName)
{
    final int nameLength = displayName.length();

    for (int i = 0; i < nameLength; i++)
    {
        final char hs = displayName.charAt(i);

        if (0xd800 <= hs && hs <= 0xdbff)
        {
            final char ls = displayName.charAt(i + 1);
            final int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;

            if (0x1d000 <= uc && uc <= 0x1f77f)
            {
                return true;
            }
        }
        else if (Character.isHighSurrogate(hs))
        {
            final char ls = displayName.charAt(i + 1);

            if (ls == 0x20e3)
            {
                return true;
            }
        }
        else
        {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff)
            {
                return true;
            }
            else if (0x2B05 <= hs && hs <= 0x2b07)
            {
                return true;
            }
            else if (0x2934 <= hs && hs <= 0x2935)
            {
                return true;
            }
            else if (0x3297 <= hs && hs <= 0x3299)
            {
                return true;
            }
            else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
            {
                return true;
            }
        }
    }

    return false;
}