Swift Basics: String data storage and length

Source: Internet
Author: User
Tags scalar

The storage mechanism for strings in Swift differs from other languages in that the characters are not stored in a fixed byte format, and each character may occupy a different number of bytes.

When we need to know how many strings a string has, we often want to know the number of characters we see, not the number of bytes stored.

A character may need to be stored 1,2,3,4 bytes.

English: 1 bytes

Xxx:2 a byte. ---didn't find an example.

Chinese: 3 bytes

Emoji expression character: 4 bytes

Charaters.count indicates how many characters, that is, we see a few characters, not care about how they are stored.

It's the same as Unicodescalars.count.



For codeunit in Language.utf8
{
    print ("Utf8:\ (codeunit)", Terminator: "")
}
print ("\ n")
for Codeunit in Language.utf16
{
    print ("Utf16:\ (codeunit)", Terminator: "")
}

results:
utf8:83 utf8:119 utf8:105 utf8:102 utf8:116 utf8:232 utf8:175 utf8:173 utf8:232 utf8:168 utf8:128 


For ease of use, we can extend the properties of string:

Custom string length computed property
extension string
{
    var length:int {return
        self.characters.count
    }
}}
Language.length   //returns 7


Let's take a look at the official example and make it clear:



This is an example of a mixture of different types of characters, the dog head is an expression character, occupies 4 bytes.


1. UTF8 expression:




You can see 10 bytes occupied, that is, DogString.utf8.count = 10.

Each unit is a UInt8 type


2. UTF16 expression: DogString.utf16.count = = 6

Each unit is unsigned int16 type


An emoticon requires 2 UInt16 representations.



3. Unicodescalar Scalar representation


DogString.unicodeScalars.count = 5


Each unit is unicodescalar, consuming UInt32 memory and holding 21bit of data.

Each unicodescalar has a value of that returns the scalar ' s 21-bit value, represented within a uint32value.



This expression is closest to what we see.


Reference Links:

Swift_programming_language:stringsandcharacters


Related Article

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.