String是一个结构对象, 如Map、Filter和Reduce等针对结构对象的经典操作也能用到字符串上。比如常用的语法
// map
let abc: String = "abc"
_ = abc.map {
print($0.description)
}
// filter
let filtered = abca.filter { $0 == "a" }
// reduce
let result = abc.reduce("1") { (result, c) -> String in
return result + String(c)
}
// map
let abc: String = "abc"
_ = abc.map {
print($0.description)
}
// filter
let filtered = abca.filter { $0 == "a" }
// reduce
let result = abc.reduce("1") { (result, c) -> String in
return result + String(c)
}
if let found = s.firstMatch(%"searchString") { ... }
if let found = s.firstMatch(someRegex) { ... }
for m in s.allMatches((%"searchString"), case: .insensitive) { ... }
for m in s.allMatches(someRegex) { ... }
let items = s.split(separatedBy: ", ")
let tokens = s.split(separatedBy: CharacterSet.whitespace)
更多资料查看:
https://github.com/apple/swift/blob/master/docs/StringManifesto.md