Swift calculates the number of characters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.