Strings

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.

"Hello World".uppercased() creates a string with the value HELLO WORLD, while "Hello World".lowercased() will create a string containing hello world.

These functions don’t modify the original string but create new ones. This is important to know so you’re aware that you need to store the newly generated string in another variable:

let exampleText = "Hello World"
let uppercaseString = exampleString.uppercased()
let lowercaseString = exampleString.lowercased()

exampleText is unmodified, and the other two variables contain the new versions of the string.

You can also create a new version of the string by replacing occurrences of words:

let exampleText = "Hello World"
let replacedWordString = exampleText.replacing("Hello", with: "Greetings")

replacedWordString contains the string Greetings World. You can also change the occurrences of a character:

let exampleText = "Hello World"
let replacedCharacterString = exampleText.replacing("l", with: "1")

replacedCharacterString contains the value He11o Wor1d.

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"

ageMessage contains the value She is 25 years old. Notice how the variable age is written inside the string. When you write a variable inside a backslash and round brackets inside a string, you tell Swift to convert the value stored in that variable to a string and make it part of the surrounding string. So in this example, Swift converts the value in age, which is 25, to be String type with the value "25".

25 and "25" aren’t the same thing. For you, they both read the same. But for the computer, the first is an integer number, while the other is a string of two characters using the English letters "2" and "5".

If you try creating a string like "She is " + age + " years old", Xcode complains and gives you an error.

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. :]

The name print is from the old days when computer apps were all command-line. So for apps to provide output to the user, they printed the output on the screen. Today, apps have fancy interfaces with buttons and colors. But there’s still this command-line, although you rarely need it. It’s known as console. And it is NOT a gaming console. :]

To use print(), all you need is to give this statement a String, and it’ll print its value in the console:

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

You can always pass the string to it directly and not through a variable:

print("This is an example string to be printed in the console")

In the next demo, you’ll create a playground that creates a greeting string to include a name, then you’ll update the calculator from the last part to print user-friendly messages about the calculation and the result.

See forum comments
Download course materials from Github
Previous: Demo Next: Strings Demo