Skip to content
Advertisement

Remove last char string when string contains emojis

I’m using React Native (chat), in this app I have one emoji picker but when users press virtual backspace button emojis only remove their last unicode byte.

How can I detect if last ‘chat’ (set of unicodes) is standalone emoji unicode?

Code to split it:

inputValue.substring(0, inputValue.length - 1)

I also tried to use Array.from(), then use slice(0,-1) and correct solutions to all cases appears when I console log it. BUT! When I set the SAME variable to the TextInput appears: 321331231221.

Some examples:

"Hello😁".length
7

"Hello😁".slice(0,-1)
'Hello�'

Array.from("Hello😁")
[ 'H', 'e', 'l', 'l', 'o', '😁' ]

Array.from("Hello😁").slice(0,-1)
[ 'H', 'e', 'l', 'l', 'o' ]

Array.from("Hello😁").slice(0,-1).join('')
'Hello'

But if I set this last solution in my React Native TextInput component I see something like this: 312412412411 (maybe all in hex). If the value has not emojis I see the same result as well.

When I set the emoji with the emoji picker, something like ${inputValue}${newEmoji} all works out.

I do not know what to think guys :/

Advertisement

Answer

Try with slice :

inputValue.slice(0, -1);

UPDATE :

This package might help you: runes

example of your need :

const runes = require('runes')

function backspace(text){

       return runes(text).slice(0, -1);

}

//the call : 

backspace('Made with ♥') // Made with
Advertisement