Created
January 12, 2017 22:32
-
-
Save ka2n/2dcb469a92be9b300de34bbb442a66e4 to your computer and use it in GitHub Desktop.
Split String into [String] in specified characters length. swift3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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