Book Image

Mastering Swift

By : Jon Hoffman
Book Image

Mastering Swift

By: Jon Hoffman

Overview of this book

Table of Contents (22 chapters)
Mastering Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The string type


A string is an ordered collection of characters, such as Hello or Swift. In Swift, the string type represents a string. We have seen several examples of strings already in this book, so the following code should look familiar. This code shows how to define two strings:

var stringOne = "Hello"
var stringTwo = " World"

Since a string is an ordered collection of characters, we can iterate through each character of a string. The following code shows how to do this:

var stringOne = "Hello"
for char in stringOne {
  println(char)
}

The preceding code will display the results shown in the following screenshot:

There are two ways to add one string to another string. We can concatenate them or we can include them inline. To concatenate two strings, we use the + or the += operator. The following code shows how to concatenate two strings. The first example appends stringB to the end of stringA and the results are put into a new stringC variable. The second example appends stringB directly...