1

Why is

"".characters.count == 1

and not equal to 3? Is there a way to get my expected result of 3?

SonBrooks
  • 54
  • 3
  • 10
  • I don't know about swift specifically, but "" is made up of 4 characters. – Alexander O'Mara Sep 07 '16 at 04:44
  • Compare also [How to know if two emojis will be displayed as one emoji?](http://stackoverflow.com/questions/39104152/how-to-know-if-two-emojis-will-be-displayed-as-one-emoji), which shows a method to get `3` as the "composed characters count" of `""`. – Martin R Sep 07 '16 at 05:00
  • @AlexanderO'Mara: `""` consists of 4 Unicode scalars, but is a single "extended grapheme cluster". Interestingly, `""` counts as s single grapheme cluster as well. – Martin R Sep 07 '16 at 05:14

1 Answers1

0

A probable cause is that Swift counts any sequence of regional indicator characters as one cluster. Your string is basically "USUSUS". But since it is displayed as three independent grapheme clusters, the count should intuitively be 3. When you put other characters between the flags, they are counted as expected.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121