Strings

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last demo, you learned how to create new variables and constants, you created a calculator with an Xcode playground and used the data types Bool, Int, Float and Double. You saw the different levels of precision between the last two types.

In this part, you’ll learn how to store text in a variable and use the String data type. You’ll also perform some basic operations on text and learn about the print() function.

In Swift, you define a variable that can store text by defining it with the String data type:

let exampleText: String = "Hello"

The text needs to be between double quotes "".

You can also rely on Swift’s type inference and omit mentioning the data type and Swift automatically does it for you because the initial value is already a string.

let exampleText = "Hello"

You can store a string of any length in a variable. It can be a single word like "Hello", or a whole book. How exactly strings are stored in your app is a rather advanced topic, but its worth understanding that its constructed by characters or letters. The string "Hello" is the group of characters "H", "e", "l", "l", and "o". The longer the string, the more characters it’ll have.

You can get the length of a string using the count property. That will give you the number of characters in this string:

exampleText.count

When exampleText contains "Hello", count will give you the count 5.

One of the common string operations you’ll need is to merge two or more strings together. This looks the same as math addition:

let exampleText = "Hello" + "World"

By adding the two words together, you get a string with the value "HelloWorld". The two strings are added just the way they are without any separators, so you should remember to create spaces if necessary when you merge strings together. A space is also considered a character:

let exampleText = "Hello" + " " + "World"

Manipulating Strings

There are many interesting operations you can use to manipulate a string. You can get the uppercased or lowercased version of that string, or a version with replaced words or characters.

let exampleText = "Hello World"
let uppercaseString = exampleString.uppercased()
let lowercaseString = exampleString.lowercased()
let exampleText = "Hello World"
let replacedWordString = exampleText.replacing("Hello", with: "Greetings")
let exampleText = "Hello World"
let replacedCharacterString = exampleText.replacing("l", with: "1")

Strings From Numbers

Another frequent operation you’ll need is to create a string that includes numbers. An example is when you want to show someone’s age after calculating it:

let age = 25
let ageMessage = "She is \(age) years old"

Print Statement

A valuable way of showing values stored in variables in your app while it’s running is the print statement. Don’t worry, it doesn’t connect to your printer and wont consume any paper. :]

let age = 25
let ageMessage = "She is \(age) years old"
print(ageMessage)
print("This is an example string to be printed in the console")
See forum comments
Download course materials from Github
Previous: Demo Next: Strings Demo