Skip to content

Instantly share code, notes, and snippets.

@d-date
Last active January 6, 2018 07:43
Show Gist options
  • Save d-date/dc6bb413465063b96e6d96d7ea303d56 to your computer and use it in GitHub Desktop.
Save d-date/dc6bb413465063b96e6d96d7ea303d56 to your computer and use it in GitHub Desktop.
extension String {
func split(_ size: Int) -> [String] {
return stride(from: 0, to: self.count, by: size).map { i -> String in
let startIndex = self.index(self.startIndex, offsetBy: i)
let endIndex = self.index(startIndex, offsetBy: size, limitedBy: self.endIndex) ?? self.endIndex
return String(self[startIndex..<endIndex])
}
}
}
print("あけましておめでとうございます。今年もどうぞよろしくお願いします。".split(5))
//["あけまして", "おめでとう", "ございます", "。今年もど", "うぞよろし", "くお願いし", "ます。"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment