String equality
Sometimes you want to determine if two strings are equal. For example, a children’s game of naming an animal in a photo would need to determine if the player answered correctly.
In Swift, you can compare strings using the standard equality operator, ==, in exactly the same way as you compare numbers. For example:
let guess = "dog"
let dogEqualsCat = guess == "cat"
Here, dogEqualsCat is a Boolean that in this case equals false, because "dog" does not equal "cat". Simple!
Just as with numbers, you can compare not just for equality, but also to determine is one value is greater than or less that another value. For example:
let order = "cat" < "dog"
This syntax checks if one string comes before another alphabetically. In this case,
order equals true because "cat" comes before "dog".
The subject of equality brings up an interesting feature of Unicode: There are two ways to represent some characters. One example is the é in café, which is an e with an acute accent. You can represent this character with either a single character or with two.
You saw the single character, code point 233, earlier in this chapter. The two- character case is an e on its own followed by an acute accent combining
character, which is a special character that modifies the previous character. So you can represent the e with an acute accent by either of these means:
The combination of these two characters in the second diagram forms what is known as a grapheme cluster.
Another example of combining characters are the special characters used to change the skin color of certain emojis. For example:
Here, the thumbs up emoji is followed by a skin tone combining character. On platforms that support it, including iOS and macOS, the rendered emoji is a single thumbs up character with the skin tone applied.
Combining characters make equality of strings a little trickier. For example, consider the word café written once using the single é character, and once using the combining character, like so:
These two strings are of course logically equal. When they are printed onscreen, they look exactly the same. But they are represented inside the computer in different ways. Many programming languages would consider these strings to be
unequal because those languages work by comparing the characters one by one. Swift, however, considers these strings to be equal by default. Let’s see that in action:
let stringA = "café"
let stringB = "cafe\u{0301}"
let equal = stringA == stringB
Note: In the code above, the acute accent combining character is written using the Unicode shorthand, which is \u followed by the code point in hexadecimal, in braces. You can use this shorthand to write any Unicode character. I had to use it here for the combining character because there’s no way to type this character on my keyboard!
In this case, equal is true, because the two strings are logically the same.
String comparison in Swift uses a technique known as canonicalization. Say that three times fast! Before checking equality, Swift canonicalizes both strings, which means they’re converted to use the same special character representation.
It doesn’t matter which way it does the canonicalization — using the single character or using the combining character — as long as both strings get converted to the same style. Once the canonicalization is complete, Swift can compare individual characters to check for equality.