How can I split the given String in Swift into groups with given length, reading from right to left?
For example, I have string 123456789 a
I made something like this, couldn't create anything better looking, but its result matches the question:
func splitedString(string: String, lenght: Int) -> [String] {
var result = [String](), count = 0, line = ""
for c in string.characters.reverse() {
count++; line.append(c)
if count == lenght {count = 0; result.append(String(line.characters.reverse())); line = ""}
}
if !line.isEmpty {result.append(String(line.characters.reverse()))}
return result.reverse()
}