檢查並比較字串
檢查字串是否為空:
if str.isEmpty {
// do something if the string is empty
}
// If the string is empty, replace it with a fallback:
let result = str.isEmpty ? "fallback string" : str
檢查兩個字串是否相等(在 Unicode 規範等價意義上 ):
"abc" == "def" // false
"abc" == "ABC" // false
"abc" == "abc" // true
// "LATIN SMALL LETTER A WITH ACUTE" == "LATIN SMALL LETTER A" + "COMBINING ACUTE ACCENT"
"\u{e1}" == "a\u{301}" // true
檢查字串是否以另一個字串開頭/結尾:
"fortitude".hasPrefix("fort") // true
"Swift Language".hasSuffix("age") // true