Swift string
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 String
.
Create a string
You can use the literal amount of a string or 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 )
The output of the above program execution is as follows:
Hello, World!
Hello, World!
Empty string
You can use empty string literals to assign values to variables or initialize a 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" )
}
The output of the above program execution is as follows:
stringA is empty
stringB is empty
String constant
You can assign a string to a variable or constant. Variables are modifiable and constants are immutable.
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 )
The output of the above program will report an error, because 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"
Insert a value into a string
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:
import Cocoa
var varA = 20
let constA = 100
var varC:Float = 20.0
var stringA = "\(varA) by \(constA) equal to \(varC * 100)"
print( stringA )
The output of the above program execution is as follows:
20 by 100 equal to 2000.0
String concatenation
Strings can be concatenated with the + sign. Examples are as follows:
import Cocoa
let constA = "Rookie Tutorial:"
let constB = "http://www.runoob.com"
var stringA = constA + constB
print( stringA )
The output of the above program execution is as follows:
Rookie Tutorial:http://www.runoob.com
String length
String length using String.count
property, an example is as follows:
The Swift 3 version uses the String.characters.count
import Cocoa
var varA = "www.runoob.com"
print( "\(varA), the length is \(varA.count)" )
The output of the above program execution is as follows:
www.runoob.com, the length is 14
String comparison
You can use ==
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" )
}
The output of the above program execution is as follows:
Hello, Swift! and Hello, World! is unequal
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) ")
}
The output of the above program execution is as follows:
UTF-8 code:
232
143
156
233
184
159
230
149
153
231
168
139
UTF-16 code:
33756
40479
25945
31243
String functions and operators
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: let myString: String = "256"
let myInt: Int? = Int(myString)
|
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. |