SWIFT — STRINGS

String Literals in Swift

Basics of String and Multiline string literals in Swift.

Srideo Prasad
8 min readSep 20, 2020
Photo by Paul Volkmer on Unsplash

String

String is a collection of characters. In Swift, a series of characters are represented by String type. Let’s have a quick look into an example below:

Now we will look into string literals.

String Literal

String literal is the sequence of characters enclosed in double quote. This can also be used for initializing the string. When a variable is initialized with string literal, Swift compiler infer it as String type. Let look at few examples:

In above example, “Hello, world!” is the string literal assigned to stringUsingLiteral variable. Swift infer this as String.

String literal is the sequence of characters enclosed in double quote.

Now as you seen in the above example, you may raise few questions like, Can we use as much number of character as we wish in the string literal? Can string literal be multi-line sentences also, like paragraphs? Can string literal provides the basic sentence formatting?

In short, can we write a story as string literal and assign it to a string variable???

Answer is YES! You can. Surprised?!!! Just read further and see these string literal features provided by Swift. Swift provides Multiline String Literals for this.

Multiline String Literals:

Three Double Quote — “””

Multiline string literal is a string or the sequence of strings spanned over multiple lines and enclosed between three double quotation marks delimiter.

Wait!!! what? Just earlier we have seen that string literals are enclosed between just a double quote, then why THREE DOUBLE QUOTATION MARK now? Yes, multiline string literals needs three double quotation marks. Let’s take a look at the example.

As you can see above there are the story is written in three lines — Line 2, 3 and 4. And we can see that it has been printed in three separate lines also — Line 10, 11 and 12.

Multiline string literal is spanned over multiple lines, enclosed in three double quotes.

New Line Concept

Now a point to be noted quickly is that, a multi-line string literal content must not begin on the line which contains the start delimiter. And in same way closing delimiter should not be on the line same line in which content ends, it must be after the line on which content ends.

Read above lines again!

In above example, can’t we start writing the story content from the line 1 itself? And, can’t we put end delimiter in same line in which story ends? NO, we can’t. If we do, the compiler will throw error. See example below:

So what happened to the blank spaces in the line 1 and will line 5? Will that also be included in the string literal or not? NO, the delimiters lines are not included in the string literals. So line 1 and line 5 will won’t be considered while the string operations.

Multiline string literals content must begin on a new line and closing delimiter must begin on a new line. Delimiter lines are not included in the string literals operations.

Avoiding Line Breaks

Let’s look at example below first,

In above example, you can see that the quoted line is actually written in two lines. This has been done by introducing new line after word “reputation,”. But don’t you think a quote looks good in single line. So how do we achieve that? That can be achieved by writing backslash “\” after word “reputation,” in above example. See below:

In above example you can see that, when backslash is introduced at the end of Line 2, then the whole statement is printed in single line (Line 9).

This is useful in cases when for better readability purposes, you may write shorter lines in source code inside the multiline string literals. For doing this you must have used line break after few words itself. But you may not want the line break in the string value or when it is displayed, in those cases you can use backslash at the end of the lines.

But what to do if the multiline string literals itself have backslash in the string content? Think! We will discuss later in this reading.

When source code includes a line break inside of a multiline string literal, that line break also appears in real string’s value. If you want to avoid the line break then use the backslash ‘\’ at the end of those lines.

Introducing Blank Line

Suppose you are writing a short story inside a multiline string literals, and you want to introduce blank line between two paragraphs, how you will achieve this? It’s simple, just introduce blank line in the source, it will replicate in the string value also. Look at the below example.

You can see that the blank line introduced at Line 8. And when string is printed, the same blank is introduced at Line 25.

Blank line in source replicates in the multiline string value also.

Line Indentation

Now we will see how we can indent the multiline strings. There are multiple cases, let first start with adding few whitespaces at the beginning of the line. If you write whitespaces before the starting of the line, then that whitespace is included in the string value.

In above example, you can see that whitespaces in first line are included in the string value.

Whitespaces before the starting of the line is included in the multiline string literal value.

The next question is how to indent whole multiline string? Well, this can be done by indenting the closing delimiter appropriately. Any space on the left of the closing delimiter is ignored in the string value. Let’s look into multiple examples below:

In above example you can see that any whitespace before closing delimiter, (grey area) is ignored while string is displayed. But any space along with or after closing delimiter is included in the string value and it is shown when string is displayed.

Any guess, what will happen in below case, when closing delimiter is itself after the multiline string literal sentences?

You can’t do this. Compiler will throw below error!

Whitespaces before the closing delimiter are ignored for all the other lines. But whitespaces along or after are included.

That’s all for the string literals! But don’t go anywhere we are not done yet. Let’s approach towards the special characters in the String literals.

Special Characters

There are certain characters which has special effects when included in the string literals. A list of special characters in Swift listed below.

Now let’s see the effects of these special characters when included in the string literals. This is bit long example, but a minute of patience will make us gain a lot. Please go through this.

As promised, let’s see how a backslash is included in the string literals. Have a look at the Line 31, that explain everything itself.

Now the next obvious question is, what if we want to remove the effect of these special characters when included in the string literals? Use Extended String Delimiters for this.

Extended String Delimiters

You can place a string literal within extended delimiters to include special characters in a string without invoking their special effects. For this you have to place the string within quotation marks and surround that with # sign. Let see the examples how to achieve this.

You can see in above example that we have placed the # sign at the start and the end of the string. And this has removed the effect of \n in the line. As a result the line didn’t break.

One other thing to be noticed is different strings has different number of delimiter sign, #. Yes, You can use any number of delimiter sign (#), but the number of delimiter sign should always be same at the start and the end of the string. It may be one, two, three, multiple in numbers.

You can use any number of delimiter sign (#), but the number of delimiter sign should always be same at the start and the end of the string.

What happen if we have multiple special characters in a string literals, but we just want to remove the effect for few of them not all? In this case use extended delimiters for the string and add the delimiters in between the special character for which you want to have the effects!

In above example you can see that whenever ## is used in between the \n like — \##n and in \t like — \##t, then in these cases special characters has its effects in the string literal, rather than printing as plain characters. One thing need to be remembered is that the number of # in between special characters should be same as at the start and end of the string literal.

Special characters have effects when included in the string literals. This effect can be removed by using Extended string delimiters.

That’s all for now!

Conclusions

  1. String literal is the sequence of characters enclosed in double quote.
  2. Multiline string literal is spanned over multiple lines, enclosed in three double quotes.
  3. Multiline string literals content must begin on a new line and closing delimiter must begin on a new line. Delimiter lines are not included in the string literals operations.
  4. Line break can be avoided in multiline string literal using the backslash ‘\’ at the end of the lines.
  5. Blank line in source replicates in the multiline string value also.
  6. Whitespaces before the starting of the line is included in the multiline string literal value.
  7. Whitespaces before the closing delimiter of multiline string are ignored for all the other lines. But whitespaces along or after are included.
  8. Special characters have effects when included in the string literals. This effect can be removed by using Extended string delimiters.

Thank You!

I hope you have enjoyed going through this!

--

--