Android Question Get emoticons (emoji) from string one-by-one

plager

Member
Hello community,
I am struggling to get emoticons from the string one-by-one.
Consider this:
I have a string with emoticons. This string is correctly displayed when displaying it as whole string .
B4X:
Dim str as string = "????????❤️"
Log(str)
It correctly show the whole string.
But when I try to iterate if one character after another, it always gives me �. or ��.
I understand, that emoticons are binary characters and cannot be represented as one byte, but they are as two bytes.
However, when I do this code:
B4X:
For i = 0 To str.Length - 1
    Log(str.CharAt(i))                ' this gives � or ��
    Log(str.Substring2(i,i+1))        ' this gives same ouput as CharAt.
Next

The idea is to create buttons for each character (emoji) in the string. When I put whole string to the button.text property, it is displayed correctly. But when I ietrate string, it gives me twice as much buttons and each of them has text as �.
Anybody has a clue how to achieve this?
Many thanks ;)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Emojies are complicated and many of them are made of multiple characters.

For simple emojies you can use this code:
B4X:
Private Sub LogEachSimpleEmoji(s As String)
    Dim b() As Byte = s.GetBytes("UTF-32LE")
    For i = 0 To b.Length - 3 Step 4
        Dim s As String = BytesToString(b, i, 4, "UTF-32LE")
        Log(s)
    Next
End Sub

If you want to handle more complex emojies then check the code in BCTextEngine (CreateBCTextCharsFromString).
 
Upvote 0

plager

Member
It works perfectly, Erel. Thank you very much for your comprehensive answer!
I have one more question: I obviously played with bytes to BytesToString function, but didn't get to coding as UTF-32LE. I tried UTF8, UTF-32BE. Where did you get UTF-32LE from? Is there any list of those codings?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
With all respect, this question is highly relative to original question, so I'd like to keep it here, please.
THIS is NOT the way this forum is designed.

As @Alexander Stolte wrote:

You should ALWAYS create a NEW THREAD for ANY NEW QUESTION.

One thread should summarize ONE question.
The Question in #1 IS ANSWERED.

Create a new thread for a new question
 
Upvote 0
Top