Gavin Wiggins

AboutNotesPhotos


Strings

Written on November 12, 2022

A string in Swift is represented by characters wrapped with double quotation marks " ". Multiline strings are wrapped with three double quotation marks """ """. Use a backslash \ to ignore a line break in a multiline string.

// A string literal

let a = "This is a string"

// Multiline string

let multi = """
This is an example
of a multiline
string
"""
// Prints the following:
// This is an example
// of a multiline
// string


// Multiline string with indentation

let indented = """
The next line
  is indented from the left.
"""
// Prints the following:
// The next line
//   is indented from the left.

// Ignore line breaks in a multiline string

let ignored = """
This is the first line \
which doesn't break.
And this is the second line.
"""
// Prints the following:
// This is the first line which doesn't break.
// And this is the second line.

Gavin Wiggins © 2024.
Made on a Mac with Genja. Hosted on GitHub Pages.