Strings and Emojis

I came across a tweet today joking about how composite emoji’s can be manipulated using Javascript. I tried to replicate the same in Swift and with interesting results.

Emoji & Unicode String manipulation

How it all started out was while trying out the new String functions. The way to iterate through all of the characters in the string, you can simply use

The Unicode Emoji 👩‍❤️‍👩 is a single character right? Not really. Take a guess. 1? 3? 6?
The space it takes is 1 character. If you try to get the length of the same, it is 3 characters in length.

it prints the length, which is 3

If you split the same into Unicode characters, take a guess…

it prints the length of unicode characters, which is 6

Let’s see what these characters are

You would notice the following output

The interesting things to note are that this one emoji is composed of a Woman, a Heart and another Woman along with some extra characters. You can read about this in detail on the Unicode pages at http://unicode.org/reports/tr51/ and http://www.unicode.org/emoji/charts/emoji-zwj-sequences.html

What we learned today…
The point of this article is as follows,
1. If you are using strings in Swift with Unicode characters, the lengths can vary and you need to know why
2. Unicode characters can be made up of composite unicode characters
3. Unicode characters can be changed to form other unicode characters

So hope you have learned a bit of these today.

Views All Time
Views All Time
445
Views Today
Views Today
1
Posted in Uncategorized.