1 you can get the number of characters in the string by calling the global countelements function and passing the string as an argument. 2Let Unusualmenagerie ="Koala??, Snail??, Penguin??, dromedary??"3println"Unusualmenagerie has \ (countelements (Unusualmenagerie)) characters")//Prints "Unusualmenagerie has characters"4 Note: Different Unicode characters and different representations of the same Unicode characters may require a different amount of memory space to store, so the characters in Swift do not necessarily occupy the same memory space in a string. As a result, the length of the string has to be computed by iterating through the length of each character in the string. If you are working with a long string, be aware that the countelements function must traverse the characters in the string to accurately calculate the length of the string. 5 6It is also important to note that the number of characters returned by Countelements is not always the same as the length property of NSString that contain the same character. The length property of the NSString is based on the use of utf- -Represents a 16-bit code unit number, rather than a Unicode character. To solve this problem, NSString's Length property becomes utf16count when accessed by Swift's String.
Swift calculates the number of characters