5

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
  • 19,406
  • 19
  • 101
  • 135
toiavalle
  • 414
  • 7
  • 20

2 Answers2

10

Swift4 has this feature built in:

var myMultilineString =
"""
This is a

multiline string
"""

very convenient

Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27
3

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,563
  • 11
  • 16
Price Ringo
  • 3,424
  • 1
  • 19
  • 33