Strings and characters
String is a collection of character (character)-type values such as "Hello, World", "Thief King", represented by a string type.
Swift's string and character types provide a fast, Unicode-compatible way to handle textual information in code. The syntax for creating and manipulating strings is similar to string manipulation in C, lightweight and easy to read. The string connection operation requires simply connecting two strings with the + sign. As with other values in Swift, you can change the value of a string, depending on whether it is defined as a constant or a variable.
Although syntax is simple, the string type is a fast, modern string implementation. Each string consists of independently encoded Unicode characters and provides support for accessing these characters in different Unicode representations (representations).
String interpolation can be done in constants, variables, literals, and expressions, making it easy to create custom strings for presentation, storage, and printing.
Attention:
The string type is seamlessly bridged with the Foundation NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch. All NSString APIs can invoke values of any string type that you create. In addition to this, you can also use the string attribute described in this chapter. You can also use string-type values as an alternative to any API that requires incoming NSString instances as parameters. For more information on using string in Foundation and Cocoa, see Using Swift with Cocoa and objective-c.
string literal (string literals)
You can include a predefined string value in your code as a string literal. String literals are wrapped in double quotation marks ("") with a fixed-order text character set.
String literals can be used to provide initial values for constants and variables.
let"Some string literal value"
Attention:
The somestring variable is initialized by the string literal, and Swift infers that the variable is of type string.
String literals can contain the following special characters:
Escape character \ (null character), \ (backslash), \ t (horizontal tab), \ n (line break), \ r (carriage return), \ "(double quotation mark), \ ' (single quote).
A single-byte Unicode scalar, written as \xnn, where nn is a two-bit hexadecimal number.
A double-byte Unicode scalar, written as \unnnn, where nnnn is a four-bit hexadecimal number.
A four-byte Unicode scalar, written as \unnnnnnnn, where nnnnnnnn is a eight-bit hexadecimal number.
The following code is an example of the use of various special characters. The Wisewords constant contains two transfer special characters (double brackets); dollarsign, Blackheart, and Sparklingheart constants demonstrate three different formats of Unicode scalars:
let"\"我是要成为海贼王的男人\" - 路飞"// "我是要成为海贼王的男人" - 路飞let"\x24" // $, Unicode 标量 U+0024let"\u2665" // , Unicode 标量 U+2665let"\U0001F496" // , Unicode 标量 U+1F496
Initializing an empty string
To construct a long string, you can create an empty string as the initial value. You can assign an empty string literal to a variable, or you can initialize a new string instance:
var"" // 空字符串字面量varString() // 初始化 String 实例// 两个字符串均为空并等价。
You can determine whether the string is empty by checking its Boolean IsEmpty property:
if emptyString.isEmpty { println("什么都没有")}// 打印输出:"什么都没有"
String variability
You can modify a specific string by assigning it to a variable, or assign it to a constant to ensure that it is not modified:
var variablestring = "Horse" variablestring + = "and carriage" //variablestring is now" Horse and carriage " let constantstring = "Highlander" constantstring + = "and another Highlander" //This reports a compilation error (compile-time error)-Constants cannot be modified.
Note:
in objective-c and Cocoa, you specify whether the string can be modified by selecting two different classes (NSString and nsmutablestring), whether the string in Swift can be modified only by defining a variable or a constant To achieve the unification of multiple types of variability operations. The
string is a value type (Strings is value Types)
Swift is a string type. If you create a new string, the value is copied when it is passed in a constant, variable assignment operation, or in a function/method. In any case, a new copy of the existing string value is created and the new copy is passed or assigned. Value types are described in structs and enumerations as value types.
Note:
Unlike nsstring in Cocoa, when you create a NSString instance in Cocoa and pass it to a function/method, or assign a value to a variable, you pass or assign a reference to the NSString instance unless you specifically want to Copy of the value, otherwise the string does not generate a new copy for the assignment operation. The way the
default string is copied guarantees that the value of the string is passed in the function/method. It's obvious that wherever that value comes from, it's all on your own. You can rest assured that the strings you pass are not changed by themselves.
at actual compile time, the Swift compiler optimizes the use of strings so that actual replication occurs only when absolutely necessary, which means that you can achieve very high performance at the same time as a value type. The
String type using characters (working with characters)
represents a collection of character (character) type values for a particular sequence. Each character value represents a Unicode character. You can use the for-in loop to iterate through each character in a string:
forcharacterin"Dog!" { println(character)}
The for-in loop is described in detail in the for Loops.
In addition, you can create an independent character constant or variable by marking a character type annotation and assigning it by character literal:
let"¥"
Calculating the number of characters
You can get the number of characters in the string by calling the global countelements function and passing the string as an argument.
let"Koala , Snail , Penguin , Dromedary "println("unusualMenagerie has \(countElements(unusualMenagerie)) characters")// 打印输出:"unusualMenagerie has 40 characters"
Attention:
Different Unicode characters and different representations of the same Unicode characters may require a different amount of memory space to store. Therefore, the characters in Swift do not necessarily occupy the same memory space in a string. So 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. It 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 NSString is based on the number of 16-bit code units represented by the UTF-16, rather than Unicode characters. To solve this problem, NSString's Length property becomes utf16count when accessed by Swift's string.
Connection strings and characters
The values of strings and characters can be added together with the addition operator (+) and create a new string value:
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 add a string or character to an already existing string variable by using the addition assignment operator (+ =):
var"look over"instruction += string2// instruction 现在等于 "look over there"var"good morning"welcome += character1// welcome 现在等于 "good morning!"
Attention:
You cannot add a string or character to a character variable that already exists because the character variable can contain only one character.
string interpolation
String interpolation is a way to construct a new string that can contain constants, variables, literals, and expressions. Each entry of the string literal that you insert is wrapped in parentheses prefixed with a backslash:
let3let"\(multiplier) 乘以 2.5 是 \(Double(multiplier) * 2.5)""3 乘以 2.5 是 7.5"
In the example above, multiplier as (multiplier) is inserted into a string literal. When you create a string to perform an interpolation calculation, this placeholder is replaced with the multiplier actual value.
Also acts as part of the subsequent expression in the string. The expression evaluates the value of double (multiplier) * 2.5 and inserts the result (7.5) into the string. In this example, the expression is written as (Double (multiplier) * 2.5) and is included in the string literal.
Attention:
Expressions written in parentheses in an interpolated string cannot contain non-escaped double quotation marks (") and backslashes (\), and cannot contain carriage returns or newline characters.
Comparing strings
Provides three ways to compare the values of strings: string equality, prefix equality, and suffix equality.
String equality
If two strings contain exactly the same characters in the same order, the strings are considered equal:
let"我们是一样一样滴."let"我们是一样一样滴."if quotation == sameQuotation { println("这两个字符串被认为是相同的")}// 打印输出:"这两个字符串被认为是相同的"
prefix/suffix equal
Checks whether a string has a specific prefix/suffix by calling the Hasprefix/hassuffix method of the string. All two methods need to pass in a string as a parameter and outgoing a Boolean value. Two methods perform a character-by-byte comparison between the base string and the prefix/suffix string.
The following example shows the position of the first two scenes of the Shakespeare play Romeo and Juliet in a string array:
Let Romeoandjuliet =["Act 1 Scene 1:verona, A Public Place","Act 1 Scene 2:capulet 'S mansion", "Act1Scene3: A-Class in Capulet' s mansion ',Act 1 Scene 4:a Street outside Capulet 'S mansion", "Act1Scene5: The great Hall in Capulet' s mansion ',"Act 2 Scene 1:outside Capulet 'S mansion", "Act2Scene2: Capulet' s Orchard ',"Act 2 Scene 3:outside friar Lawrence 'S cell", "Act2Scene4: A Street in Verona", "Act2Scene5: Capulet' s mansion ',"Act 2 Scene 6:friar Lawrence 'S cell"]
You can use the Hasprefix method to calculate the number of scenes in the first scene of a play:
var0forin romeoAndJuliet { if scene.hasPrefix("Act 1 ") { ++act1SceneCount }}println("There are \(act1SceneCount) scenes in Act 1")// 打印输出:"There are 5 scenes in Act 1"
Similarly, you can use the Hassuffix method to calculate the number of scenes that occur in different places:
var0var0forin romeoAndJuliet { if scene.hasSuffix("Capulet‘s mansion") { ++mansionCount elseif scene.hasSuffix("Friar Lawrence‘s cell") { ++cellCount }}println("\(mansionCount) mansion scenes; \(cellCount) cell scenes")// 打印输出:"6 mansion scenes; 2 cell scenes”
Uppercase and lowercase strings (uppercase and lowercase Strings)
You can access the uppercase/lowercase version of the string by using the Uppercasestring and LowerCaseString properties of the string.
let"Could you help me, please?"let shouty = normal.uppercaseString// shouty "COULD YOU HELP ME, PLEASE?"let whispered = normal.lowercaseString// whispered "could you help me, please?"
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's string and character types are fully compatible with the Unicode standard, and it supports a range of different Unicode encodings as described below. Each of the characters in
Unicode terminology (Unicode terminology)
can be interpreted as one or more Unicode scalars. The Unicode scalar of a character is a unique 21-bit number (and name), such as u+0061 for lowercase Latin alphabet A ("a"), u+1f425 for Chick expression
When a Unicode string is written into a text file or other storage structure, these Unicode scalars is encoded in one of the centralized formats defined by Unicode. It includes UTF-8 (encoded in 8-bit code units) and UTF-16 (encoded in 16-bit code units). The Unicode representation of the
string (Unicode representations of Strings)
Swift provides several different ways to access the Unicode representation of a string.
You can use For-in to traverse a string to access each character value as a Unicode character. The procedure is described in the use of characters.
In addition, the value of the string can be accessed in three other Unicode-compliant ways:
Code unit Collection (using the UTF8 property of the string to access the
Code unit collection (using the string's Utf16 property for access
bits of the Unicode label A collection of measures (using the Unicodescalars property of the string to access the
below by D o
g "! Each character in a string consisting of the DOG face, a Unicode scalar of u+1f436, represents a different representation:
let"Dog!"
UTF-8
You can access its UTF-8 representation by traversing the UTF8 property of the string. It is an attribute of type Utf8view, Utf8view is a collection of unsigned 8-bit (UInt8) values, and each UInt8 value is a UInt8 representation of a character:
forin dogString.utf8 { print("\(codeUnit) ")}print("\n")// 68 111 103 33 240 159 144 182
In the above example, the first four 10 binary code unit values (68, 111, 103, 33) represent the characters d o G and! , their UTF-8 representation is the same as the ASCII representation. The following four code unit values (240, 159, 144, 182) are the 4-byte UTF-8 representation of the dog face.
UTF-16
You can access its UTF-16 representation by traversing the Utf16 property of the string. It is an attribute of type Utf16view, Utf16view is a collection of unsigned 16-bit (UInt16) values, and each UInt16 is a UTF-16 representation of a character:
forin dogString.utf16 { print("\(codeUnit) ")}print("\n")// 68 111 103 33 55357 56374
Similarly, the first four code unit values (68, 111, 103, 33) represent the characters d o G and! , their UTF-16 code units and UTF-8 are identical.
The five and Sixth code unit values (55357 and 56374) are UTF-16 representations of the dog face character. The first value is u+d83d (decimal value is 55357) and the second value is u+dc36 (decimal value is 56374).
Unicode scalar
You can access its Unicode scalar representation by traversing the Unicodescalars property of the string. It is an attribute of type Unicodescalarview, and Unicodescalarview is a collection of unicodescalar. The Unicodescalar is a 21-bit Unicode code point.
Each unicodescalar has a value attribute and can return the corresponding 21-bit value, expressed in UInt32.
forin dogString.unicodeScalars { print("\(scalar.value) ")}print("\n")// 68 111 103 33 128054
Similarly, the first four code unit values (68, 111, 103, 33) represent the characters d o G and!. The fifth digit value, 128054, is a decimal representation of a hexadecimal 1f436. It is equivalent to the Unicode scalar u+1f436 of the dog face.
As an alternative to querying character-valued properties, each Unicodescalar value can also be used to construct a new string value, such as in string interpolation:
forin dogString.unicodeScalars { println("\(scalar) ")}
iOS development language Swift Getting started serial---strings and characters