Swift string


Swift String is a collection of characters. For example, "Hello, World!" is a collection of ordered character type values, and its data type is String.


Create a string

You can create a string by using a string literal or an instance of the 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!

Empty String

You can use an empty string literal to assign to a variable or initialize an instance of the String class to initialize an empty string. We can use the string attribute isEmpty to determine whether 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 execution output of the above program is:

stringA 是空的
stringB 是空的

String constant

You can Assign a string to a variable or constant. Variables are modifiable, while constants are unmodifiable.

import Cocoa

// stringA 可被修改
var stringA = "php中文网:"
stringA += "http://www.php.cn"
print( stringA )

// stringB 不能修改
let stringB = String("php中文网:")
stringB += "http://www.php.cn"
print( stringB )

The execution output of the above program will report an error, thinking that stringB is a constant and cannot be modified:

error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant
stringB += "http://www.php.cn"

Inserted value in string

String interpolation is A way to construct new strings that can contain constants, variables, literals, and expressions. Each item of the string literal you insert is enclosed in parentheses prefixed with a backslash:

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 be connected by + characters. The example is as follows:

import Cocoa

let constA = "php中文网:"
let constB = "http://www.php.cn"

var stringA = constA + constB

print( stringA )

The output result of the above program execution is:

php中文网:http://www.php.cn

characters String length

The string length is calculated using the String.characters.count property. The example is as follows:

import Cocoa

var varA   = "www.php.cn"

print( "\(varA), 长度为 \(varA.characters.count)" )

The output result of the execution of the above program is:

www.php.cn, 长度为 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) 与 \(varB) 是相等的" )
} else {
   print( "\(varA) 与 \(varB) 是不相等的" )
}

The output result of the above program execution is:

Hello, Swift! 与 Hello, World! 是不相等的

Unicode String

Unicode is an international standard for encoding text. Swift's String type is based on Unicode. You can loop and iterate out the UTF-8 and UTF-16 encodings in the string. The example is as follows:

import Cocoa

var unicodeString   = "php中文网"

print("UTF-8 编码: ")
for code in unicodeString.utf8 {
   print("\(code) ")
}

print("\n")

print("UTF-16 编码: ")
for code in unicodeString.utf16 {
   print("\(code) ")
}

The output result of the execution of the above program is:

UTF-8 编码: 
232 
143 
156 
233 
184 
159 
230 
149 
153 
231 
168 
139 
UTF-16 编码: 
33756 
40479 
25945 
31243

String functions and operations Symbol

Swift supports the following string functions and operators:

61011##12##<13!=
Serial numberFunction/Operator&Description
1

isEmpty

Determine whether the string is empty and return a Boolean value

2

hasPrefix(prefix: String)

Check whether the string has a specific prefix

3

hasSuffix(suffix: String)

Checks whether a string has a specific suffix.

4

Int(String)

Convert string numbers to integers. Example:

let myString: String = "256"
let myInt: Int? = Int(myString)


5

##String.characters.count

Calculate the length of the string

##utf8

You can access the UTF-8 encoding of a String by iterating over its utf8 property

7

utf16

You can access the UTF-16 of a String by iterating over its utf8 property Encoding

8

unicodeScalars

You can access this by iterating over the unicodeScalars property of the String value Its Unicode scalar encoding.

9

##+

concatenates two strings, and returns a new string

+=

concatenation operator on both sides string and assign the new string to the operator variable on the left

==

Determine whether two strings are equal

To compare two strings, compare the letters of the two strings one by one.

Compares two strings to see if they are equal.