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 countElements
number of characters returned is not always the same as the property that contains the same character NSString
length
. NSString
length
is based on the 16-bit code unit number represented by the UTF-16, not on Unicode characters. In order to solve this problem, NSString
the length
properties String
will become when accessed by Swift utf16count
.
Connection strings and characters (concatenating 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 // 等于 "hello!"let stringPlusString = string1 + string2 // 等于 "hello there"let characterPlusString = character1 + string1 // 等于 "!hello"let characterPlusCharacter = character1 + character2 // 等于 "!?"
You can also +=
add a string or character to a string variable that already exists through the addition assignment operator ():
var instruction = "look over"instruction += string2// instruction 现在等于 "look over there"var welcome = "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)
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:
let multiplier = 3let message = "\(multiplier) 乘以 2.5 是 \(Double(multiplier) * 2.5)"// message 是 "3 乘以 2.5 是 7.5"
In the example above, multiplier
as \(multiplier)
is inserted into a string literal. This placeholder is replaced with the actual value when the string is created to perform an interpolation calculation multiplier
.
multiplier
Also acts as part of the subsequent expression in the string. The value that the expression evaluates Double(multiplier) * 2.5
and inserts the result (7.5) into the string. In this example, the expression is written \(Double(multiplier) * 2.5)
and included in the string literal.
Attention:
Expressions written in parentheses in your interpolated string cannot contain non-escaped double quotation marks ( "
) and backslashes ( \
), and cannot contain carriage returns or line feeds.
Compare strings (comparing Strings)
Swift provides three ways to compare the values of strings: strings are equal, prefixes are equal, and suffixes are equal.
strings equal (string equality)
If two strings contain exactly the same characters in the same order, the strings are considered equal:
let quotation = "我们是一样一样滴."let sameQuotation = "我们是一样一样滴."if quotation == sameQuotation { println("这两个字符串被认为是相同的")}// 打印输出:"这两个字符串被认为是相同的"
prefix/suffix equal (Prefix and Suffix Equality)
hasPrefix
hasSuffix
checks whether a string has a specific prefix/suffix by invoking the/method of the string. All two methods need to pass in and out of the value as a string argument Boolean
. 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", "Act 1 Scene 3: A room in 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 hasPrefix
the method to calculate the number of scenes in the first scene of a play:
var act1SceneCount = 0for scene in 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 hasSuffix
methods to calculate the number of scenes that occur in different places:
“var mansionCount = 0var cellCount = 0for 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")// 打印输出:"6 mansion scenes; 2 cell scenes”
Uppercase and lowercase strings (uppercase and lowercase Strings)
You can uppercaseString
lowercaseString
access the uppercase/lowercase version of the string through the and properties of the string.
let normal = "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.
Unicode terminology (Unicode terminology)
Each character in Unicode 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 a U+0061
lowercase Latin alphabet A ("a"), which U+1F425
indicates the chicken expression ("")
When a Unicode string is written into a text file or other storage structure, these Unicode scalars are 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).
Unicode representation of a string (Unicode representations of Strings)
Swift provides several different ways to access the Unicode representation of a string.
You can use the traversal of a for-in
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-compatible ways:
- UTF-8 Code Unit Collection (accessed using the properties of the string
utf8
)
- UTF-16 Code Unit Collection (accessed using the properties of the string
utf16
)
- 21-bit Unicode scalar value collection (accessed using the properties of the string
unicodeScalars
)
D
o
g
!
??
DOG FACE
U+1F436
each character in a string consisting of and (, Unicode scalar) represents a different representation:
let dogString = "Dog!??"
UTF-8
You can access the representation of a string by traversing utf8
its properties UTF-8
. A UTF8View
property of type, which UTF8View
is a collection of unsigned 8-bit ( UInt8
) values, each UInt8
of which is a UTF-8 representation of a character:
for codeUnit in 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 representations are identical to the ASCII representation. The following four code unit values (240, 159, 144, 182) are DOG FACE
the 4-byte UTF-8 representation.
UTF-16
You can access the representation of a string by traversing utf16
its properties UTF-16
. A UTF16View
property of type, which UTF16View
is a collection of unsigned 16-bit ( UInt16
) values, each UInt16
of which is a UTF-16 representation of a character:
for codeUnit in 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 exactly the same.
The five and Sixth code unit values (55357 and 56374) are DOG FACE
UTF-16 representations of the characters. The first value is U+D83D
(decimal value is 55357) and the second value is U+DC36
(decimal value is 56374).
Unicode scalar (Unicode scalars)
You can unicodeScalars
access the Unicode scalar representation of a string by traversing its properties. The collection that is the property of the UnicodeScalarView
type UnicodeScalarView
UnicodeScalar
. UnicodeScalar
is a 21-bit Unicode code point.
Each one UnicodeScalar
has a value attribute, which can be represented by returning a corresponding 21-bit value UInt32
.
for scalar in 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. Its equivalent to DOG FACE
the Unicode scalar u+1f436.
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:
for scalar in dogString.unicodeScalars { println("\(scalar) ")}// D// o// g// !// ??