Skip to content

Instantly share code, notes, and snippets.

@ka2n
Created January 12, 2017 22:32
Show Gist options
  • Save ka2n/2dcb469a92be9b300de34bbb442a66e4 to your computer and use it in GitHub Desktop.
Save ka2n/2dcb469a92be9b300de34bbb442a66e4 to your computer and use it in GitHub Desktop.
Split String into [String] in specified characters length. swift3
public extension String {
public func chunkByLength(_ count: UInt) -> [String] {
var result = [String]()
var idx = characters.startIndex
while idx < characters.endIndex {
let next = characters.index(idx, offsetBy: Int(count), limitedBy: characters.endIndex) ?? characters.endIndex
if idx == next {
break
}
result.append(self[idx..<next])
idx = next
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment