extension String {
func components(withLength length: Int) -> [String] {
return stride(from:0, to: self.count, by: length).map {
let start = self.index(self.startIndex, offsetBy: $0)
let end = self.index(start, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex
return String(self[start ..< end])
}
}
}
let str = "123456789"
let comp1 = str.components(withLength: 4)
let comp2 = str.components(withLength: 3)
dump(comp1)
dump(comp2)