检查并比较字符串
检查字符串是否为空:
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