Swift Strings: Concatenation

Join strings with +, or use interpolation to insert values into a string.


Concatenate vs Interpolate

Use + to make a new string. Use interpolation to insert values inline.

Example

let first = "Hello"
let second = "Swift"
// Concatenation
print(first + " " + second)
// Interpolation
print("\(first), \(second)!")

Interpolation is often clearer when mixing text and values.



Append Strings

Use += to append to a mutable string.

Example

var s = "Hello"
s += ", Swift"
print(s)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *