Skip to content

Instantly share code, notes, and snippets.

@gchumillas
Created November 3, 2022 20:06
Show Gist options
  • Save gchumillas/6ca438974caa4f3e49b8e869bdac8aba to your computer and use it in GitHub Desktop.
Save gchumillas/6ca438974caa4f3e49b8e869bdac8aba to your computer and use it in GitHub Desktop.
extension String {
func chunks(_ size: Int) -> [String] {
stride(from: 0, to: self.count, by: size).map {
let i0 = self.index(self.startIndex, offsetBy: $0)
let i1 = self.index(self.startIndex, offsetBy: min($0 + size, self.count))
return "\(self[i0..<i1])"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment