連線字串

使用+運算子連線字串以生成新字串:

let name = "John"
let surname = "Appleseed"
let fullName = name + " " + surname  // fullName is "John Appleseed"

使用 += 複合賦值運算子或使用方法附加到可變字串 :

let str2 = "there"
var instruction = "look over"
instruction += " " + str2  // instruction is now "look over there"

var instruction = "look over"
instruction.append(" " + str2)  // instruction is now "look over there"

在可變字串中附加單個字元:

var greeting: String = "Hello"
let exclamationMark: Character = "!"
greeting.append(exclamationMark) 
// produces a modified String (greeting) = "Hello!"

將多個字元附加到可變字串

var alphabet:String = "my ABCs: "
alphabet.append(contentsOf: (0x61...0x7A).map(UnicodeScalar.init)
                                         .map(Character.init) )
// produces a modified string (alphabet) = "my ABCs: abcdefghijklmnopqrstuvwxyz"

Version >= 3.0

appendContentsOf(_:) 已更名為 append(_:)

使用 joinWithSeparator(_:) 加入一串字串以形成一個新字串 :

let words = ["apple", "orange", "banana"]
let str = words.joinWithSeparator(" & ")

print(str)   // "apple & orange & banana"

Version >= 3.0

joinWithSeparator(_:) 已更名為 joined(separator:)

separator 預設是空字串,所以 ["a", "b", "c"].joined() == "abc"