Welcome to Swift (Apple's official Swift document translation and annotations 19)---123~132 page (chapter II). This section has 6 pages left)

Source: Internet
Author: User

Working with characters (character related)

In Swift, the string type represents the value of a set of ordered characters. Each character is a Unicode symbol. You can use the for-in loop to iterate through each character in a string:

For character in "Dog!??" {

println (character)

}

D

O

G

// !

// ??

In Swift, you can also use the character type to explicitly create a single-character constant or variable:

Let Yensign:character = "¥"

Counting characters (count of characters)

Calling the global function countelements, passing a string as its argument, can calculate the number of characters in the string:

Let Unusualmenagerie = "Koala??, Snail??, Penguin??, dromedary??"

println ("Unusualmenagerie has \ (countelements (Unusualmenagerie)) characters")

Prints "Unusualmenagerie has characters"

  Note the point:

  Different Unicode characters, as well as different representations of the same Unicode characters, require different memory storage space. Therefore, the character and the string representing the character sometimes have different storage space. So since, a string of The length must be calculated based on each character traversing it. If you are dealing with a particularly long string, be aware that in order to accurately calculate the long height of the string, make sure that the Countelements function iterates through each character.

The number of characters returned by the Countelements function is inconsistent with the number of characters represented by the length property of NSString. The length of NSString is calculated based on the number of 16-bit cell codes in the string encoded in the UTF-16 format, not Calculated based on the number of Unicode characters in the string. In this case, the length property of NSString is considered as Utf16count in Swift.

Concatenating Strings and characters (link strings and characters)

String and character values can be linked by a plus sign (+) to create a new string:

Let string1 = "Hello"

Let string2 = "there"

Let Character1:character = "!"

Let Character2:character = "?"

Let Stringpluscharacter = string1 + character1//equals "hello!"

Let stringplusstring = string1 + string2//equals "Hello there"

Let characterplusstring = character1 + string1//equals "!hello"

Let Characterpluscharacter = Character1 + character2//equals "!?"

You can also append a string or character to an already defined string by using the compound addition operator (+ =).

var instruction = "Look over"

Instruction + = string2

Instruction now equals ' look over there '

var welcome = "Good Morning"

Welcome + = Character1

Welcome now equals "Good morning!"

  Note the point:

You cannot append a string or character to a character variable, because a value of one character type can only store one character.

string interpolation (string concatenation insert operation)

The concatenation of strings is a way of building new strings by mixing the values of the organization constants, variables, text, and expressions in this string literal. Each entry inserted into the string literal is wrapped in parentheses () and preceded by a slash (\):

Let multiplier = 3

Let message = "\ (multiplier) times 2.5 is \ (Double (multiplier) * 2.5)"

Message is ' 3 times 2.5 is 7.5 '

In the above example, the value of multiplier is inserted into the string literal by \ (multiplier). When the string insertion operation is performed, the location of \ (multiplier) is replaced by the actual value of multiplier, creating an unambiguous string.

The value of the multiplier at the end of the string is part of a long expression. The expression evaluates the value of Double (multiplier) * 2.5 and then inserts the computed result value (7.5) into the string. In the example, use \ (Double (multiplier) * 2.5) method to include the value in the string.

  Note the point:

The expression in parentheses () cannot contain double quotation marks or backslashes \, nor can it contain carriage returns or newline characters.

Comparing Strings (string comparison)

There are three ways to compare strings in Swift: strings are equal, prefixes are equal, and suffixes are equal

String equality (string equality)

If two strings contain the same sequence of characters, then the two strings are equal:

Let quotation = "We ' re a lot alike, you and I."

Let samequotation = "We ' re a lot alike, you and I."

If quotation = = Samequotation {

println ("These, strings is considered equal")

}

Prints "These, strings is considered equal"

Prefix and Suffix equality (prefix and suffix equal)

Call the Hasprefix and Hassuffix methods of the string to check whether a string has the specified prefix or suffix string. Both methods take only one parameter of type string and return a Boolean type value. This method will be compared by character-by-word.

In the following code example, the string array represents the first two chapters of Shakespeare's Romeo and Juliet (Shakespeare's Romeo and Juliet):

Let Romeoandjuliet = [

"Act 1 Scene 1:verona, A public place",

"Act 1 Scene 2:capulet ' s mansion",

Act 1 Scene 3: A Capulet ' s mansion ",

"ACT 1 Scene 4:a Street outside Capulet ' s mansion",

"Act 1 Scene 5:the great Hall in Capulet ' s mansion",

"Act 2 Scene 1:outside Capulet ' s mansion",

"Act 2 Scene 2:capulet ' s Orchard",

"Act 2 Scene 3:outside friar Lawrence ' s Cell",

"ACT 2 Scene 4:a Street in Verona",

"Act 2 Scene 5:capulet ' s mansion",

"Act 2 Scene 6:friar Lawrence ' s Cell"

]

You can use the Hasprefix method to calculate the number of scenes in this drama Chapter 1:

var act1scenecount = 0

For scene in Romeoandjuliet {

if Scene.hasprefix ("Act 1") {

++act1scenecount

}

}

println ("There is \ (Act1scenecount) scenes in Act 1")

Prints "There is 5 scenes in Act 1"

Similarly, you can use the Capulet ' s mansion and Friar Lawrence's cell as parameters to calculate the number of scenes by Hassuffix method:

var mansioncount = 0

var cellcount = 0

For scene in Romeoandjuliet {

if Scene.hassuffix ("Capulet ' s Mansion") {

++mansioncount

} else if Scene.hassuffix ("Friar Lawrence ' s cell") {

++cellcount

}

}

println ("\ (mansioncount) mansion scenes; \ (cellcount) cell scenes ")

Prints "6 mansion scenes; 2 cell scenes "

Uppercase and lowercase Strings (case conversion of strings)

With the uppercasestring and LowerCaseString properties, you can store access in uppercase or lowercase:

Let normal = "Could to help me, please?"

Let shouty = normal.uppercasestring

shouty is equal to "COULD do help ME, please?"

Let whispered = normal.lowercasestring

Whispered is equal to "could do help me, please?"

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.