Swift Learning notes-strings and characters (Strings and characters)-unicode

Source: Internet
Author: User
Tags scalar


Unicode


Unicode is an international standard for encoding and representation of text. It enables you to represent almost all characters from any language in a standard format and to read and write to characters in external resources such as text files or Web pages. Swift'sStringandCharactertype are fully compatible with the Unicode standard.





Unicode scalar (Unicode scalars)


TheStringtype of Swift is based on Unicode scalars . A Unicode scalar is a unique 21-digit number that corresponds to a character or modifier, such as aU+0061Latin alphabet ("") that represents a lowercase letter ("")LATIN SMALL LETTER Aa, whichU+1F425indicates a chicken expression () ("FRONT-FACING BABY CHICK??").


Note: The range of Unicode code poing isU+0000toU+D7FForU+E000toU+10FFFF. Unicode scalars do not include Unicode surrogate (surrogate pair) code bits, whose code-bit range isU+D800toU+DFFF.


Note that not all 21-bit Unicode scalars represent one character, because some scalars are reserved for future allocations. A scalar that already represents a typical character has its own name, such as the and in the example aboveLATIN SMALL LETTER AFRONT-FACING BABY CHICK.





string literal special character (special characters in string literals)


String literals can contain the following special characters:


    • Escape character\0(null character), (backslash),\\\t(Horizontal tab),\n(line break),\r(carriage return),\"(double quotation mark),\‘(single quotation mark).
    • A Unicode scalar, written in\u{n}lowercase (u), whichnis any one to eight hexadecimal digits and the available Unicode bit codes.


The following code is an example of the use of various special characters. wiseWordsA constant contains two double quotation marks.dollarSign,blackHeartandsparklingHeartconstants demonstrate three different formats of Unicode scalars:


let wiseWords = "\" Imagination is more important than knowledge \ "-Einstein"
// "Imageination is more important than knowledge"-Enistein
let dollarSign = "\ u {24}" // $, Unicode scalar U + 0024
let blackHeart = "\ u {2665}" // ♥, Unicode scalar U + 2665
let sparklingHeart = "\ u {1F496}" // ??, Unicode scalar U + 1F496







Extensible Glyph Cluster (Extended grapheme Clusters)


Each SwiftCharactertype represents an extensible glyph group. An extensible Glyph group is one or more ordered permutations of Unicode scalars that produce human-readable characters. For example, a letterécan be represented by a single Unicode scalaré(LATIN SMALL LETTER E WITH ACUTE, orU+00E9). A standard lettere(LATIN SMALL LETTER Eor), however, is a scalar ()U+0065with a quick accent (), so that aCOMBINING ACTUE ACCENTU+0301pair of scalars represents the same letteré. The scalar image of this shortness of stress is to beeconvertedé.



In both cases, the letter represents aésingle SwiftCharactervalue and represents an extensible glyph group. In the first case, the glyph group contains a single scalar, whereas in the second case it is a two scalar group of glyphs:


let eAcute: Character = "\ u {E9}" // é
let combinedEAcute: Character = "\ u {65} \ u {301}" // e followed by?
// eAcute is é, combinedEAcute is é





The extensible character clouds set is a flexible way to represent a single value with a number of complex script charactersCharacter. For example, Korean phonetic energy saving from the Korean alphabet is expressed as an ordered arrangement of combinations or decomposition. In Swift will be expressed as a singleCharactervalue:


let precomposed: Character = "\ u {D55C}" //?
let decomposed: Character = "\ u {1112} \ u {1161} \ u {11AB}" //?,?,?
// precomposed is?, decomposed is?





The extensible character clouds set can surround other Unicode scalars with enclosing tokens (for exampleCOMBINING ENCLOSING CIRCLEU+20DD, or), as a singleCharactervalue:


let regionalIndicatorForUS: Character = "\ u {1F1FA} \ u {1F1F8}"
// regionalIndicatorForUS is ????





The Unicode scalar of the local indicator symbol can be combined into a singleCharactervalue, such asREGIONAL INDICATOR SYMBOL LETTER U(U+1F1FA) andREGIONAL INDICATOR SYMBOL LETTER S(U+1F1F8):


Let Regionalindicatorforus:character = "\u{1f1fa}\u{1f1f8}"//Regionalindicatorforus is????




Calculate the number of characters (counting characters)


If you want to get the number of values in a stringCharacter, you can use the properties of the string'scharacterspropertiescount:


let unusualMenagerie = "Koala ??, Snail ??, Penguin ??, Dromedary ??"
print ("unusualMenagerie has \ (unusualMenagerie.characters.count) characters")
// printout "unusualMenagerie has 40 characters"





Note In Swift, when you use the extensible character clouds set as aCharactervalue to concatenate or change a string, it does not necessarily change the number of characters in the string.



For example, if you initialize a new string with a four-character wordcafe, then add aCOMBINING ACTUE ACCENT(U+0301) as the end of the string. Eventually the number of characters in this string is still4, because the fourth character is, instead ofée:


var word = "cafe"
print ("the number of characters in \ (word) is \ (word.characters.count)")
// prints "the number of characters in cafe is 4"

word + = "\ u {301}" // COMBINING ACUTE ACCENT, U + 0301

print ("the number of characters in \ (word) is \ (word.characters.count)")
// prints "the number of characters in café is 4" 




Note: The extensible character clouds set can form one or more Unicode scalars. This means that different characters and different representations of the same characters may require a different amount of memory space to store. Therefore, the characters in Swift do not necessarily occupy the same amount of memory space in a string. Therefore, the number of characters in a string cannot be computed without the scope of the extensible character clouds of the string. If you are working with a long string, you need to note that thecharactersproperty must traverse all Unicode scalars to determine the number of characters in the string.

It is also important to note that thecharactersnumber of characters returned by a property is not always the same as the property that contains the same characterNSStringlength.NSStringis a property of alength16-bit code unit number represented by UTF-16, not a Unicode extensible word clouds set. As a corroboration, whenNSStringalengthproperty is accessed by a swiftStringvalue, it is actually calledutf16Count.


Swift Learning notes-strings and characters (Strings and characters)-unicode


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.