Create Multiline string in Swift
This tutorial will show us how to create a multiline string in Swift.
A multi-line string is a string with more than one line. As we all know to declare a string, we need to enclose the string within double quotes(” “) and the string is created.
But if we give try to create a multi-line string with double quotes just by pressing the enter key, it will not work. Here is a way that can be used to create a multi-line string in Swift.
Implementation and Explanation
As there is mentioned above double quotes won’t work in multi-line strings, so the alternative way to create a multi-line string is-
Using a line break(“\n”) at the end of the first line would accomplish our task to declare and it will work as-
var twolines="line 1\n" + "line 2" print(twolines)
line 1 line 2
The other method we can create a multiline string is by using triple quotes(“””), here is the snippet part-
var lines=""" line 1 line 2 line 3 """ print(lines)
line 1 line 2 line 3
You just need to make sure that the triple quotes are not on the same line as of string.
This is how we can create a multi-line string in Swift. I hope you found this tutorial helpful.