Android Question Check if there are only emojis in the string

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

Is there an easy way to check if only emojis in a string?

I would like to check this to highlight it by changing the text size, like Instagram where you can see the emojis in large size when sending only one emoji or two.

Greetings
 

DonManfred

Expert
Licensed User
Longtime User
Use RegEx to find Matches (emois).
Or compare the given text to a list of emojis and replace them with something other if yound...
:)
;-)
;)
:D
and so on...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Get the string code points and compare them to several known ranges:
B4X:
   Dim s As String = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602) & "a"
   Dim cp() As Int = StringToCodePoints(s)
   For Each point In cp
       Log(IsEmojiCodePoint(point))
   Next

Sub IsEmojiCodePoint (cp As Int) As Boolean
   Return (cp >= 0x1F600 And cp <= 0x1F64F) Or _
       (cp >= 0x1F300 And cp <= 0x1F5FF) Or _
       (cp >= 0x1F680 And cp <= 0x1F6FF) Or _
       (cp >= 0x1F1E6 And cp <= 0x1F1FF) Or _
       (cp >= 0x2600 And cp <= 0x26FF) Or _
       (cp >= 0x2700 And cp <= 0x27BF) Or _
       (cp >= 0xFE00 And cp <= 0xFE0F) Or _
       (cp >= 0x1F900 And cp <= 0x1F9FF) Or _
       (cp >= 65024 And cp <= 65039) Or _
       (cp >= 8400 And cp <= 8447)
End Sub

Sub StringToCodePoints(s As String) As Int()
   Dim b() As Byte = s.GetBytes("UTF-32BE")
   Dim bc As ByteConverter
   Return bc.IntsFromBytes(b)
End Sub

Sub UTS (codepoint As Int) As String
   Dim bc As ByteConverter
   Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
   Return BytesToString(b, 0, 4, "UTF-32BE")
End Sub
The ranges source: https://stackoverflow.com/questions/30757193/find-out-if-character-in-string-is-emoji?rq=1
 
Last edited:
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
What is the output?
Output of only one emoji (heart):
B4X:
false
-131072: false
false
1680277504: false
Output of only text ("a"):
B4X:
false
-131072: false
false
1744830464: false
Output of one emoji plus text (heart plus "a"):
B4X:
false
-131072: false
false
1744830464: false
false
1680277504: false

and the output of your example:
UTS(0x1F600) & UTS(0x1F601) & UTS(0x1F602) & "a"
B4X:
false
-131072: false
false
16122112: false
false
32899328: false
false
49676544: false
false
1627389952: false
 
Upvote 0
Top