stringswift

How to write a multiline string in Swift?


How could I split a string over multiple lines such as below?

var text:String = "This is some text
                   over multiple lines"

Solution

  • Swift 4 includes support for multi-line string literals. In addition to newlines they can also contain unescaped quotes.

    var text = """
        This is some text
        over multiple lines
        """
    

    Older versions of Swift don't allow you to have a single literal over multiple lines but you can add literals together over multiple lines:

    var text = "This is some text\n"
             + "over multiple lines\n"