Swift getting started (II) -- character and string, swift getting started

Source: Internet
Author: User

Swift getting started (II) -- character and string, swift getting started
Swift character Definition

The characters in Swift are somewhat different from OC. Each character represents an extensible letter set. The extensible letter set is represented by several (greater than or equal to one) Unicode scalar values, these scalar forms an extensible letter set, that is, a human-readable character.

Unicode scalar

A Unicode scalar occupies 21 bits, generally in the format of "U + xxxx". This explanation is a bit abstract. In fact, a letter we generally understand can also be represented by a Unicode scalar, for example, the letter 'A' can be expressed as "U + 0061 ".

As mentioned above, a character is represented by several Unicode scalar values. For example:

let combine1 = "\u{1112}"let combine2 = "\u{1112}\u{1161}"let combine3 = "\u{1112}\u{1161}\u{11AB}"println("combine1 = \(combine1)")println("combine2 = \(combine2)")println("combine3 = \(combine3)")

The output result is

Combine1 = bytes
Combine2 = commandid
Combine3 = too many bytes

It can be seen that a character is not necessarily composed of a Unicode scalar. For example, "zookeeper ".

Derivation of Character Types

In the previous example, we defined three constants: combine0, combine1, and combine2. Why are they deduced? The answer is the String type rather than the Character type. I guess it is caused by Swfit's wide type priority. Here, we will verify the Code:

Var combine0 = "\ u {1112}" var combine1: character = "\ u {1112}" combine0 + = "abc" // correct combine1 + = "abc" // compilation error: Unable to splice String and Character Variables
Character concatenation

When initializing a character type constant or variable, Swift automatically Concatenates the Unicode scalar that can be concatenated into a human-readable character, A string consisting of multiple characters is generated. Assigning this value to a variable marked as Character will cause compilation errors.

Var string = "\ u {1112} \ u {10 FFFF}" // correct. THE string variable is derived as the String type var c: character = "\ u {1112} \ u {10 FFFF}" // error, cannot be assigned to Character type variable
Swift string Definition

The string in Swift consists of several characters.

String Initialization

There are two methods for string initialization:

var emptyString1 = ""var emptyString2 = String()

In this way, an empty string is created and can be verified using the isEmpty attribute of the string:

if emptyString1.isEmpty{    println("emptyString1 is empty")}if emptyString2.isEmpty{    println("emptyString2 is empty")}

The output result is:

emptyString1 is emptyemptyString2 is empty
String concatenation

Strings can be added, and the left side of the equal sign must be a variable, not a constant.
String variables can also call the append method to connect other characters. Note that the append method can only connect characters and cannot connect strings.

Var combine1 = "\ u {1112}" let combine2 = "\ u {1112} \ u {1161}" combine1 + = combine2 // correct combine2 + = combine1 // error var char: character = "a" combine1.append (char) // correct combine1.append ("a") // an error is returned even if it is written as this way. The cause is unknown and may be derived as a String type.
String count

Because a character is composed of several Unicode scalar values, you cannot use the string size divided by the size of a single string to calculate the string length. You can use the count method to calculate the string length:

let stringValue1 = "Hello, world"let stringValue2 = "\u{65}\u{301}"println("string1 length = \(count(stringValue1))")println("string2 length = \(count(stringValue2))")println("string2 = \(stringValue2)")

As mentioned before, the running result is as follows:

String1 length = 12
String2 length = 1
String2 = e Branch

String Interpolation

This is similar to the NSString stringWithFormat method in OC. You can use other variables such as existing strings or numbers to construct a new string. An example is provided to demonstrate the following:

Var number = 3var time = 2var setence = "\ (number) \ (time) times \ (number * time)" println ("setence = \ (setence )")

The method corresponding to OC replaces "% @" @".
Note that the interpolation expression cannot directly contain double quotation marks, single quotation marks, or backslash.

String comparison

Through = and! = Operator to compare whether strings are equal

When comparing two strings, swift does not compare each Unicode scalar to be equal one by one.Actual Semantics. In the following example, pay special attention to the comparison results:

Var compare1 = "caf \ u {E9}" // \ u {E9} is an e with a tone, e. var compare2 = "caf \ u {65} \ u {301}" // This is the letter e that is combined with the tone. if compare1 = compare2 {// They still equal println ("\ (compare1) is equal to \ (compare2 )")} var compare3 = "\ u {41}" // Latin letter Avar compare4 = "\ u {0410}" // The slavic letter Aif compare3! = Compare4 {// although the performance is the same, the actual semantics is different, so the string is still different from println ("\ (compare3) is not equal to \ (compare4 )")}

The running result is as follows:

Café is equal to cafe Workshop
A is not equal to assign

Prefix and suffix of a string

The hasPrefix and hasSuffix methods are used to determine whether the string contains a forward or backward fix. A Bool type value is returned. Note that these two methods are case sensitive.

var preFixAndSuffix = "Hello World"if preFixAndSuffix.hasPrefix("H"){    println("\(preFixAndSuffix) has prefix \"H\"")}if preFixAndSuffix.hasPrefix("h"){    println("\(preFixAndSuffix) has prefix \"h\"")}if preFixAndSuffix.hasSuffix("rld"){    println("\(preFixAndSuffix) has suffix \"rld\"")}if preFixAndSuffix.hasSuffix("rldd"){    println("\(preFixAndSuffix) has suffix \"rldd\"")}

Output result:

Hello World has prefix "H"Hello World has suffix "rld"
Appendix Swift getting started series tutorials Swift getting started (1) -- basic syntax Swift getting started (3) -- Tuple Swift getting started (4) -- Optionals and Assert)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.