2017-07-31 34 views

回答

1
let world = "Hello, world!".characters.suffix(6).dropLast() 
String(world) // → "world" 

這裏分裂,它返回子序列的陣列,也用於字符串處理。它的定義那樣:

extension Collection { 
func split(maxSplits: Int = default, 
    omittingEmptySubsequences: Bool = default, 
    whereSeparator isSeparator: (Self.Iterator.Element) throws -> Bool) rethrows 
    -> [AnySequence<Self.Iterator.Element>] 
} 

對於例如

let commaSeparatedArray = "a,b,c".characters.split { $0 == "," } 
commaSeparatedArray.map(String.init) // → ["a", "b", "c"] 

對於更詳細的split in swift 3