4

How can I create a multiline string in Swift? This is what I tried:

var myMultilineString = "This is a " + "\n" + "multiline string"
print(myMultilineString)

I wanted it to print:

This is a

multiline string

Dan Beaulieu
  • 18,664
  • 18
  • 94
  • 132
toiavalle
  • 404
  • 6
  • 20

2 Answers2

5

Swift4 has this feature built in:

var myMultilineString =
"""
This is a

multiline string
"""

very convenient

Ciprian Rarau
  • 2,800
  • 1
  • 29
  • 26
2

If you want more than a line return between the strings but rather a real blank line, then you need two line feed characers.

var myMultilineString = "This is a " + "\n\n" + "multiline string"
print(myMultilineString)

enter image description here

Josh
  • 1,553
  • 11
  • 16
Price Ringo
  • 3,326
  • 1
  • 18
  • 33