Swift string
A Swift string is a collection of characters. F or example, a collection of values for an ordered character type such as "Hello, World!" with a data type of String.
Create a string
You can create a string by using a literal string or an instance of a String class:
import Cocoa // 使用字符串字面量 var stringA = "Hello, World!" print( stringA ) // String 实例化 var stringB = String("Hello, World!") print( stringB )
The output of the above program execution is:
Hello, World! Hello, World!
An empty string
You can literally assign an empty string to a variable or initialize an instance of a String class to initialize an empty string. We can use the string property isEmpty to determine if the string is empty:
import Cocoa // 使用字符串字面量创建空字符串 var stringA = "" if stringA.isEmpty { print( "stringA 是空的" ) } else { print( "stringA 不是空的" ) } // 实例化 String 类来创建空字符串 let stringB = String() if stringB.isEmpty { print( "stringB 是空的" ) } else { print( "stringB 不是空的" ) }
The output of the above program execution is:
stringA 是空的 stringB 是空的
String constant
You can assign a string to a variable or constant, which is modifiable and the constant is not modifiable.
import Cocoa // stringA 可被修改 var stringA = "W3Cschool教程:" stringA += "http://www.w3cschool.cn" print( stringA ) // stringB 不能修改 let stringB = String("W3Cschool教程:") stringB += "http://www.w3cschool.cn" print( stringB )
The output of the above program execution will report an error, assuming that stringB is a constant that cannot be modified:
error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant stringB += "http://www.w3cschool.cn"
Insert a value in the string
String interpolation is a way to build a new string that contains constants, variables, literals, and expressions. Each item of the literal amount of string you insert is in parentheses with a backslash prefix:
import Cocoa var varA = 20 let constA = 100 var varC:Float = 20.0 var stringA = "\(varA) 乘于 \(constA) 等于 \(varC * 100)" print( stringA )
The output of the above program execution is:
20 乘于 100 等于 2000.0
String connection
Strings can + by a plus sign, as follows:
import Cocoa let constA = "W3Cschool教程:" let constB = "http://www.w3cschool.cn" var stringA = constA + constB print( stringA )
The output of the above program execution is:
W3Cschool教程:http://www.w3cschool.cn
The length of the string
String length is calculated using the String.count property, as follows:
Swift 3 uses String.characters.count
import Cocoa var varA = "www.w3cschool.cn" print( "\(varA), 长度为 \(varA.characters.count)" )
The output of the above program execution is:
www.w3cschool.cn, 长度为 14
String comparison
You can == two strings equal by using . . .
import Cocoa var varA = "Hello, Swift!" var varB = "Hello, World!" if varA == varB { print( "\(varA) 与 \(varB) 是相等的" ) } else { print( "\(varA) 与 \(varB) 是不相等的" ) }
The output of the above program execution is:
Hello, Swift! 与 Hello, World! 是不相等的
Unicode string
Unicode is an international standard for text encoding, and Swift's String type is based on Unicode. You can loop through the encoding of UTF-8 and UTF-16 in the string, as follows:
import Cocoa var unicodeString = "W3Cschool教程" print("UTF-8 编码: ") for code in unicodeString.utf8 { print("\(code) ") } print("\n") print("UTF-16 编码: ") for code in unicodeString.utf16 { print("\(code) ") }
The output of the above program execution is:
UTF-8 编码: 232 143 156 233 184 159 230 149 153 231 168 139 UTF-16 编码: 33756 40479 25945 31243
String functions and operators
Swift supports the following string functions and operators:
Serial number | Functions/operators and descriptions |
---|---|
1 |
isEmpty To determine if the string is empty, a Boolean value is returned |
2 |
hasPrefix(prefix: String) Check that the string has a specific prefix |
3 |
hasSuffix(suffix: String) Check if the string has a specific suffix. |
4 |
Int(String)
The conversion string number is an integer.
Instance:
let myString: String = "256" let myInt: Int? = Int(myString) |
5 |
String.count
Swift 3 uses String.characters.count
Calculate the length of the string |
6 |
utf8 You can access String's UTF-8 encoding by traversing String's utf8 property |
7 |
utf16 You can access String's UTF-16 encoding by traversing String's utf8 property |
8 |
unicodeScalars You can access its Unicode scalar code by traversing the unicodeScalars property of the String value. |
9 |
+ Connect the two strings and return a new string |
10 |
+= Connect strings on both sides of the operator and assign a new string to the operator variable on the left |
11 |
== Determines whether the two strings are equal |
12 |
< Compare two strings, one by one, for the letters of the two strings. |
13 |
!= Compare whether the two strings are not equal. |