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

let str = "Hello, learn swift for fun"
for char in str.characters {
    print(char)
}

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.

print("👩‍❤️‍👩".characters.count)

it prints the length, which is 3

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

print("👩‍❤️‍👩".unicodeScalars.count)

it prints the length of unicode characters, which is 6

Let’s see what these characters are

for e in "👩‍❤️‍👩".unicodeScalars {
    print(e, e.value)
}

You would notice the following output

👩 128105
‍ 8205
❤ 10084
️ 65039
‍ 8205
👩 128105

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
1603
Views Today
Views Today
1
Posted in Uncategorized.