Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Forked from erica/split.swift
Created August 26, 2015 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natecook1000/66d1b9dad869e597d19f to your computer and use it in GitHub Desktop.
Save natecook1000/66d1b9dad869e597d19f to your computer and use it in GitHub Desktop.
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id"
extension String {
func split(separator: Character, maxSplit: Int = .max, allowEmptySlices: Bool = false) -> [String] {
return characters.split(separator, maxSplit: maxSplit, allowEmptySlices: allowEmptySlices).map(String.init)
}
}
let words = try string.split(" ")
let counts = words.map { $0.characters.count }
print(counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment