Swift string
Swift is a set of strings of characters. For example, the set of values of character type "Hello, World!" So ordered, its data typeString.
Creating strings
You can create an instance of the string by using a string literal or String class:
import Cocoa // 使用字符串字面量 var stringA = "Hello, World!" print( stringA ) // String 实例化 var stringB = String("Hello, World!") print( stringB )
The above program execution output is:
Hello, World! Hello, World!
Empty string
You can use an empty literal string assigned to the variable or initialize an instance of the String class to the initial value of an empty string. We can use isEmpty string property to determine whether a 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 above program execution output is:
stringA 是空的 stringB 是空的
String Constants
You can assign a string to a variable or constant variable is modifiable, constants can not be modified.
import Cocoa // stringA 可被修改 var stringA = "本教程:" stringA += "https://w3big.com" print( stringA ) // stringB 不能修改 let stringB = String("本教程:") stringB += "https://w3big.com" print( stringB )
The output of the above program execution error will result, that stringB constant can not be modified:
error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant stringB += "https://w3big.com"
Insert the string value
String interpolation is a new way to build a string that can contain constants, variables, literals, and expressions therein. Each insert your string literals are prefixed with a backslash in parentheses:
import Cocoa var varA = 20 let constA = 100 var varC:Float = 20.0 var stringA = "\(varA) 乘于 \(constA) 等于 \(varC * 100)" print( stringA )
The above program execution output is:
20 乘于 100 等于 2000.0
String concatenation
Strings can be connected bya + sign, examples are as follows:
import Cocoa let constA = "本教程:" let constB = "https://w3big.com" var stringA = constA + constB print( stringA )
The above program execution output is:
本教程:https://w3big.com
String length
String length is calculated usingString.characters.count properties, examples are as follows:
import Cocoa var varA = "w3big.com" print( "\(varA), 长度为 \(varA.characters.count)" )
The above program execution output is:
w3big.com, 长度为 14
String comparison
You can use== to compare two strings are equal:
import Cocoa var varA = "Hello, Swift!" var varB = "Hello, World!" if varA == varB { print( "\(varA) 与 \(varB) 是相等的" ) } else { print( "\(varA) 与 \(varB) 是不相等的" ) }
The above program execution output is:
Hello, Swift! 与 Hello, World! 是不相等的
Unicode strings
Unicode is an international standard for encoding text, Swift's String type is based on the Unicode built. You can loop iteration the string encoding UTF-8 and UTF-16, the examples are as follows:
import Cocoa var unicodeString = "本教程" print("UTF-8 编码: ") for code in unicodeString.utf8 { print("\(code) ") } print("\n") print("UTF-16 编码: ") for code in unicodeString.utf16 { print("\(code) ") }
The above program execution output 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:
No. | Function / Operator & Description |
---|---|
1 | isEmpty To determine whether the string is empty, it returns a Boolean value |
2 | hasPrefix (prefix: String) Check whether the string has a specific prefix |
3 | hasSuffix (suffix: String) Check whether the string has a specific suffix. |
4 | Int (String) Digital converts the string to an integer. Example: let myString: String = "256" let myInt: Int? = Int(myString) |
5 | String.characters.count Calculating the length of the string |
6 | utf8 You can access it by walking the UTF-8 encoding of utf8 String property |
7 | utf16 You can access it by walking the UTF-16 encoding of utf8 String property |
8 | unicodeScalars You can access it by walking encoded Unicode scalar unicodeScalars attribute String value. |
9 | + Concatenate two strings and returns a new string |
10 | + = Both sides of the string concatenation operator and the new operator assigned to the string variable on the left |
11 | == Determine whether two strings are equal |
12 | < Compare two strings, two strings of letters by-side comparison. |
13 | ! = Compares two strings? Are not equal. |