Swift Strings

A Swift string is a collection of a series of characters. For example, an ordered collection of character-type values such as "Hello, World!", whose data type isString。


Creating Strings

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 Strings

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

stringA 是空的
stringB 是空的

String Constants

You can assign a string to a variable or a constant. Variables are modifiable, while constants are not modifiable.

import Cocoa

// stringA 可被修改
var stringA = "Example:"
stringA += "http://www.example.com"
print( stringA )

// stringB 不能修改
let stringB = String("Example:")
stringB += "http://www.example.com"
print( stringB )

The output of the above program execution will result in an error because 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.example.com"

String Interpolation

String interpolation is a way to construct a new string, which can include constants, variables, literals, and expressions. Each item of the string literal you insert is within parentheses prefixed by 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 Concatenation

Strings can be concatenated using the+operator, as shown in the following example:

import Cocoa

let constA = "Example:"
let constB = "http://www.example.com"

var stringA = constA + constB

print( stringA )

The output of the above program execution is:

Example:http://www.example.com

String Length

String length is calculated using theString.countproperty, as shown in the following example:

The Swift 3 version uses String.characters.count

import Cocoa

var varA   = "www.example.com"

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

The output of the above program execution is:

www.example.com, 长度为 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 of the above program execution is:

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

Unicode Strings

Unicode is an international standard used for text encoding. Swift's String type is built based on Unicode. You can iterate over the UTF-8 and UTF-16 encodings in a string, as shown in the following example:

import Cocoa

var unicodeString   = "Example"

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 No. Function/Operator & Description
1

isEmpty

Checks whether the string is empty, returns a Boolean value

2

hasPrefix(prefix: String)

Checks whether the string has a specific prefix

3

hasSuffix(suffix: String)

Checks whether the string has a specific suffix.

4

Int(String)

Converts a numeric string to an integer. Example:

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

5

String.count

The Swift 3 version uses String.characters.count

Calculates the length of the string

6

utf8

You can access a String's UTF-8 encoding by iterating over its utf8 property

7

utf16

You can access a String's UTF-16 encoding by iterating over its utf16 property

8

unicodeScalars

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

9

+

Concatenates two strings and returns a new string

10

+=

Concatenates the strings on both sides of the operator and assigns the new string to the variable on the left of the operator

11

==

Determines whether two strings are equal

12

<

Compares two strings, comparing the characters of the two strings one by one.

13

!=

Determines whether two strings are not equal.

Other Extensions