paint-brush
Getting Started With Strings in Swiftby@ishaanbedi
2,439 reads
2,439 reads

Getting Started With Strings in Swift

by Ishaan BediFebruary 17th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, we'll be looking at the concept of Strings in Swift. Strings, as the name suggests, are a thread/collection of characters, woven together to form a word or a sentence. To use a string in Swift, we enclose the word or the sentence inside double-quotes. To get double quotes inside our string, just put a backslash before the double quote inside the string, and you are good to go. To interpolate a variable or a constant inside a string, we use a back-slash followed by a constant wrapped inside a pair of parentheses.

Company Mentioned

Mention Thumbnail
featured image - Getting Started With Strings in Swift
Ishaan Bedi HackerNoon profile picture


In this article, we'll be looking at the concept of Strings in Swift.


Strings, as the name suggests, are a thread/collection of characters, woven together to form a word or a sentence.

To initialize a string in Swift, we enclose the word or the sentence inside double-quotes. We may initialize string either as a variable or a constant, depending upon our need.


Example:

let myName = "Ishaan"


In this code snippet, we have declared a constant myName and we have stored a string value of "Ishaan" in it.

Easy, as it gets :)

Multiline strings in Swift

What if we want to store a poem or some text where we have to use multiple lines. The first syntax you'll guess in your mind would be something like this:

let myPoem = "Hello,
We haven't talked in quite some time
I know
I haven't been the best
Of sons, hello, I've been traveling in the desert of my mind
And I Haven't found a drop"

But this is the wrong syntax and Swift will raise an error while compiling the code.


Screenshot showing error As you can see, Swift did not compile our code and showed us errors.


The solution to this problem is to wrap our string inside three double-quotes instead of using double-quotes one time.

let myPoem = 
""""
Hello,
We haven't talked in quite some time
I know
I haven't been the best
Of sons, hello, I've been traveling in the desert of my mind
And I Haven't found a drop
"""

Now, this is the perfect syntax and we have successfully created a multi-line string in Swift.


Screenshot showing multi-line strings


And we can see that Swift compiled our code without any error this time.

Including a Double Quote in our String

What if we want to include double quotes inside our string, for example, we have to store a conversation inside our string.

If we use our familiar syntax by wrapping our double quotes inside double quotes, Swift will get confused about which double quote is wrapping whom (too much confusion, I know). As you can see from the following screenshot as well that our program has not been compiled.

Screenshot showing double quote error


Let's get it sorted:


To get double quotes inside our string, just put a backslash before the double quote inside the string, and you are good to go.

Screenshot showing correct syntax for double quotes inside a string As you can see from the above code snippet, we can see that after introducing backslash before double quotes, our program has successfully compiled and is working as per our satisfaction.


String Interpolation in Swift

The next crucial part in understanding strings in Swift is understanding the concept of String Interpolation.


To interpolate a variable or a constant inside a string, we use a backslash followed by a variable or a constant wrapped inside a pair of parenthesis, and it's done.

Let's look at the following code snippet to understand better.


Screenshot showing string interpolation example.


As you can see, we have interpolated strings inside a variable myDetails by including the constant myName & the variable myAge in it.

We can insert any kind of raw Swift code inside \( ), be it be calculations, values of any kind of data type, or a reference from another variable/constant. You may see below code snippets as an example for this concept:


Screenshot showing string interpolation example



Screenshot showing string interpolation example


So, that's all for this article. Hope you've got a deep understanding of strings in Swift. We'll be looking at some advanced string methods and operations in an upcoming article.


Thank you for reading this far.


First Published here