美文网首页
Swift4中 String类型的更新

Swift4中 String类型的更新

作者: Maggie的小蜗居 | 来源:发表于2017-07-02 22:13 被阅读46次

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

相关文章

网友评论

      本文标题:Swift4中 String类型的更新

      本文链接:https://www.haomeiwen.com/subject/sppocxtx.html