A Swift string is a collection of characters. For example, “Hello, World!”a collection of values of such an ordered character type, whose data type is You can use the literal amount of a string or The output of the above program execution is as follows: You can use empty string literals to assign values to variables or initialize a The output of the above program execution is as follows: You can assign a string to a variable or constant. Variables are modifiable and constants are immutable. The output of the above program will report an error, because String interpolation is a way to build new strings that can include constants, variables, literals, and expressions. Each item of the literal amount of the string you insert is in parentheses prefixed with a backslash: The output of the above program execution is as follows: Strings can be concatenated with the + sign. Examples are as follows: The output of the above program execution is as follows: String length using The Swift 3 version uses the The output of the above program execution is as follows: You can use The output of the above program execution is as follows: The output of the above program execution is as follows: Swift supports the following string functions and operators: Serial number Function / operator & description 1 Determines whether the string is empty and returns a Boolean value 2 Check whether the string has a specific prefix 3 Check whether the string has a specific suffix. 4 Convert string numbers to integers. Example: 5 The Swift 3 version uses String.characters.count Calculate the length of a string 6 You can access the UTF-8 encoding of String by traversing its utf8 property 7 You can access the utf16 encoding of String by traversing its utf8 property 8 You can access its Unicode scalar encoding by iterating through the unicodeScalars property of the string value. 9 Concatenate two strings and return a new string 10 Concatenate the strings on both sides of the operator and assign the new string to the operator variable on the left 11 Judge whether two strings are equal 12 Compare two strings and compare the letters of the two strings one by one. 13 Compare whether two strings are not equal.
String
. 9.24.1. Create a string #
String
class to create a string:import Cocoa
// Using string literals
var stringA = "Hello, World!"
print( stringA )
// String instantiate
var stringB = String("Hello, World!")
print( stringB )
Hello, World!
Hello, World!
9.24.2. Empty string #
String
class to initially value an empty string. We can use string properties
isEmpty
to determine whether the string is empty:import Cocoa
// Create an empty string using string literals
var stringA = ""
if stringA.isEmpty {
print( "StringA is empty" )
} else {
print( "StringA is not empty" )
}
// Instantiating the String class to create an empty string
let stringB = String()
if stringB.isEmpty {
print( "stringB is empty" )
} else {
print( "stringB is not empty" )
}
stringA is empty
stringB is empty
9.24.3. String constant #
import Cocoa
// stringA can be modified
var stringA = "Rookie Tutorial:"
stringA += "http://www.runoob.com"
print( stringA )
// stringB can not be modified
let stringB = String("Rookie Tutorial:")
stringB += "http://www.runoob.com"
print( stringB )
stringB
being a constant cannot be modified:error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant
stringB += "http://www.runoob.com"
9.24.4. Insert a value into a string #
import Cocoa
var varA = 20
let constA = 100
var varC:Float = 20.0
var stringA = "\(varA) by \(constA) equal to \(varC * 100)"
print( stringA )
20 by 100 equal to 2000.0
9.24.5. String concatenation #
import Cocoa
let constA = "Rookie Tutorial:"
let constB = "http://www.runoob.com"
var stringA = constA + constB
print( stringA )
Rookie Tutorial:http://www.runoob.com
9.24.6. String length #
String.count
property, an example is as follows:
String.characters.count
import Cocoa
var varA = "www.runoob.com"
print( "\(varA), the length is \(varA.count)" )
www.runoob.com, the length is 14
9.24.7. String comparison #
==
to compare whether two strings are equal:import Cocoa
var varA = "Hello, Swift!"
var varB = "Hello, World!"
if varA == varB {
print( "\(varA) and \(varB) is equal" )
} else {
print( "\(varA) and \(varB) is not equal" )
}
Hello, Swift! and Hello, World! is unequal
9.24.8.
Unicode
String #
Unicode
is an international standard for text encoding, Swift
String
the type is based on
Unicode
it was built. You can iterate through the string.
UTF-8
and
UTF-16
an example of the code is as follows:import Cocoa
var unicodeString = "Rookie Tutorial"
print("UTF-8 code: ")
for code in unicodeString.utf8 {
print("\(code) ")
}
print("\n")
print("UTF-16 code: ")
for code in unicodeString.utf16 {
print("\(code) ")
}
UTF-8 code:
232
143
156
233
184
159
230
149
153
231
168
139
UTF-16 code:
33756
40479
25945
31243
9.24.9. String functions and operators #
isEmpty
hasPrefix(prefix:
String)
hasSuffix(suffix:
String)
Int(String)
let myString: String = "256"
let myInt: Int? = Int(myString)
String.count
utf8
utf16
unicodeScalars
+
+=
==
<
!=