Swift4中 String类型的更新

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文: https://github.com/ecomfe/spec/blob/master/javascript...
    zock阅读 3,408评论 2 36
  • 为一个点失去了整个世界,怎么可以,要做最好的自己才对! 九月,最忙碌的季节,收收心,办正事啦!
    happyMia阅读 169评论 0 0
  • “小姐,前面好像塞车了。”的士司机虽没有转过头来,话却是说给坐在后排的美君听的。美君从自己的发愣中回过神来,伸长脖...
    浪迹天涯马小泡阅读 341评论 0 1
  • 文|小跑娘亲 春暖花开,万物复苏,小伙伴们是不是按捺不住捂了一冬的痴心妄想,呼朋引伴开始约了? 别急,听过“菜花黄...
    主妇煮米阅读 624评论 1 3