Detect if character in String is a smiley (using Android)

As the name says. I want to find out if a given java line contains an emoticon.

I can not use Character.UnicodeBlock.of(char) == Character.UnicodeBlock.EMOTICONS, because it requires API level 19.

I found this code for iOS , but it is not applicable, since it looks like java and objective-c handles surrogate pairs in different ways.

The docs I 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

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

According to Wikipedia, a set of emotions lies between 0x1f600 and 0x1f64f, but I don’t know how to check if char is in this range.

I was hoping something like this would work, but it doesn’t

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

So how do I do this?

+4
2

iOS . , , , , , 2. , , .

, else if (substring.length > 1) iOS, , Character.isHighSurrogate(myChar) .

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;
}
+1

...

if (Integer.parseInt("1f600", 16) <= (int)'☺' && (int)'☺' <= Integer.parseInt("1f64f", 16)) {
    Print.d("Unicode", "groovy!");
}

, hexidecimal char ints.

0

Source: https://habr.com/ru/post/1523972/


All Articles