Android Question Check if there are only emojis in the string

Discussion in 'Android Questions' started by Alexander Stolte, Mar 29, 2018.

  1. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed 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
     
  2. DonManfred

    DonManfred Expert Licensed 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...
     
    Alexander Stolte and Star-Dust like this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Get the string code points and compare them to several known ranges:
    Code:
    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 <= 0x1F64FOr _
           (cp >= 
    0x1F300 And cp <= 0x1F5FFOr _
           (cp >= 
    0x1F680 And cp <= 0x1F6FFOr _
           (cp >= 
    0x1F1E6 And cp <= 0x1F1FFOr _
           (cp >= 
    0x2600 And cp <= 0x26FFOr _
           (cp >= 
    0x2700 And cp <= 0x27BFOr _
           (cp >= 
    0xFE00 And cp <= 0xFE0FOr _
           (cp >= 
    0x1F900 And cp <= 0x1F9FFOr _
           (cp >= 
    65024 And cp <= 65039Or _
           (cp >= 
    8400 And cp <= 8447)
    End Sub

    Sub StringToCodePoints(s As StringAs 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, 04"UTF-32BE")
    End Sub
    The ranges source: https://stackoverflow.com/questions/30757193/find-out-if-character-in-string-is-emoji?rq=1
     
    Last edited: Apr 1, 2018
  4. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    No matter what I write in the text box the result is always "false" :confused:
     

    Attached Files:

  5. Erel

    Erel Administrator Staff Member Licensed User

    Change the log line to:
    Code:
    Log(point & ": " & IsEmojiCodePoint(point))
    What is the output?
     
  6. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Output of only one emoji (heart):
    Code:
    false
    -
    131072false
    false
    1680277504false
    Output of only text ("a"):
    Code:
    false
    -
    131072false
    false
    1744830464false
    Output of one emoji plus text (heart plus "a"):
    Code:
    false
    -
    131072false
    false
    1744830464false
    false
    1680277504false
    and the output of your example:
    Code:
    false
    -
    131072false
    false
    16122112false
    false
    32899328false
    false
    49676544false
    false
    1627389952false
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Alexander Stolte and DonManfred like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice