I wonder why I am getting different results in each of the following lines
let spain = "España"
print (spain.count) -- return 6
print (spain.utf8.count) -- return 7
print (spain.utf16.count) -- return 6
print (spain.utf8CString.count) -- return 8
I wonder why I am getting different results in each of the following lines
let spain = "España"
print (spain.count) -- return 6
print (spain.utf8.count) -- return 7
print (spain.utf16.count) -- return 6
print (spain.utf8CString.count) -- return 8
The count
in String
and in String.UTF8View
, String.UTF16View
etc are different because their way of storage is different although they are all collections. String
stores them in a collection of characters, so each item in the collection represents one character. However, in other encodings, they are stored in a collection of that encoding's code units. Some special characters (like ñ) have to be stored in two units. For ñ, it is 0xC3 0xB1
in UTF8 while it is 0x00F1
in UTF16, so you can see that there is one more item in the UTF8 encoding than UTF16. You cannot get the number of characters accurately with count
unless you are using the base String
.