Main image of article Swift Tutorial: Getting Started with Strings

Swift is a fun, simple language to pick up, and strings are one of the basic principles of the language. That may lead you to think that, in Swift, strings are pretty simple. And they can be! But there’s also a lot of complexity. 

So let’s dive in. We’ll go over the basics of Swift strings, specific to how most developers will use them when creating an app. (If you're just getting into Swift for the first time, also check out our breakdowns of Swift Package Manager and the details of Swift 5.1.)

What’s a String?

Strings in Swift are pretty simple… and easy to create. Here’s a very basic Swift string:

let sampleString = “Hello”

Yup. That’s a string. Not too intimidating. This is an immutable string, because ‘let’ defines it as a constant. You can’t change this string unless you instantiate it as ‘var’:

var sampleString = “Hello”

Why do ‘let’ and ‘var’ matter? We’ll get to that, don’t worry!

A few things to keep in mind about strings: first, they must always be in quotation marks, as seen above—the quotation marks are what make them strings. Otherwise, Xcode (or whatever IDE you’re using) thinks you’re trying to do something else.

Second, if you want to create an empty string (another thing we’ll get to…), just have double quotation marks assigned as a variable, like so:

var sampleString = “”

Swift String Mutability

The term ‘mutability’ might be a bit scary if you’re just learning Swift. Sadly, it’s not an X-Men reference—but it does make Swift strings a little bit more powerful.

If you look up the meaning of ‘mutability,’ you’ll see it just means the ability for something to be changed. With that in mind, how do you mutate a Swift string? First, declare it as a variable (var):

var sampleString = “Hello”

‘Var’ is important because it allows for mutability. A ‘let’ string cannot be changed in-line; to change a constant string in Swift, you have to change the string itself. To change a variable Swift string, all you have to do is call it and assign a new value:

var sampleString = “Hello”
sampleString = “Hello, friends”
//sampleString is now “Hello, friends”

If you’d simply like to append a string (i.e., add something to the end), use the “+=“ operator. Here’s a different take:

var sampleString = “Hello”
sampleString += “ , friends”
//sampleString is now “Hello, friends”

Pro tip: remember, strings have spaces. If you play close attention, you’ll see I added a space before the comma in “friends” so it prints correctly. Otherwise, “Hello, friends” would print as “Hello,friends” and that’s ugly.

If you assigned sampleString as a constant (‘let,’ remember), mutating it would be impossible. As far as Swift is concerned, anything assigned a ‘let’ value is set in stone. But you can blend the two strings. Here’s how!

Concatenation Nation

Concatenating is another word you may not have seen before learning Swift, and like a lot of the terms in programming, it seems the engineers in charge of naming schemes read the dictionary beforehand. Concatenation just means the ability to chain things together—like strings!

First, let’s create a list of usernames as constants, then a variable greeting:

let user1 = “Nick”
let user2 = “Neil”
let user3 = “Nate”

var greeting = “Hey there, “

(Notice the spacing after the comma!)

To greet Nick, you’d call the greeting, then use the ‘+=‘ operator to add ‘user1’:

greeting += user1
//prints “Hey there, Nick”

If you wanted to greet Nick and Neil, you’d create a new variable, and concatenate your brains out:

var newGreeting = greeting + user1 + user2

But this looks weird. It prints “Hey there, NickNeil,” which is all sorts of wrong. To get really clever, you’ll need to interpolate strings.

Swift String Interpolation

Yup, look up “interpolation” if it’s a new phrase for you: it just means to insert something of a different nature in a different spot.

To get a newGreeting that looks right, you have to interpolate. Here we’ll use parentheses and a backslash. The backslash can be viewed as a wedge, and the parentheses as a wrapper for a value. First, we’ll revisit all of our code for this exercise:

let user1 = “Nick”
let user2 = “Neil”
let user3 = “Nate”

var greeting = “Hey there, “

To return a greeting that reads correctly, let’s interpolate on newGreeting:

var newGreeting = “\(greeting)\(user1) & \(user2)! Oh, there’s \(user3), too! Hey guys!”
//prints “Hey there, Nick and Neil. Oh, there’s Nate, too! Hey guys!”

Pro tip: Notice ‘\(greeting)’ and ‘\(user1)’ have no space between them. That’s because ‘greeting’ has a space, so none is needed. Be mindful of these things when interpolating strings (and adjust as needed).

Interpolation is cool, but have you ever written a paragraph (in a programming context)? Let’s try it.

Multi-Line String Literals

Have you ever played one of those choose-your-own-adventure games that emulate the old books? They’re basically a series of multi-line string literals in Swift with a few buttons (and hopefully some animations).

If you need to say more than “hello” with strings, use three quotation marks. This designates it’s a lot of text. Here’s what it looks like:

let movieQuote = “””
If you have a milkshake. And I have a milkshake. And if I have a straw... My straw reaches acrooooooooooooooss the room, and starts to drink your milkshake. I drink your milkshake! I drink it up!
“””

That’s a cool little speech from “There Will Be Blood,” but what about some dramatic pauses? You’d skip lines if you want space:

let movieQuote = “””
If you have a milkshake. And I have a milkshake. 

And if I have a straw, my straw reaches acrooooooooooooooss the room, and starts to drink your milkshake.

I drink your milkshake! I drink it up!
“””

These long-form text snippets will print exactly as they’re typed, so keep that in mind. To make these text snippets easier to read in your IDE, use backslashes. A backslash lets Xcode know that what’s below it is a continuation of the line above. An easier-to-read version of the movie quote above would look something like this:

let movieQuote = """
If you have a milkshake.\
And I have a milkshake.

And if I have a straw,\
My straw reaches acrooooooss the room,\
and starts to drink your milkshake.\

I drink your milkshake! I drink it up!
""" 

That’s simpler to read in a coding environment, but prints the exact same!

Swift Strings Are Fun!

We’ve covered the basics of Swift strings here, but there’s a lot more to them. You can tinker with Unicode, count characters, and index strings. Those features have their place, and we’ll dig into them in a different article. 

This article is a primer on how strings work in Swift, and we encourage you to open up a playground in Xcode and start tinkering! There’s nothing quite like toying with features to refresh your skillset or learn a new concept.