Split String into groups with specific length

后端 未结 10 1775
囚心锁ツ
囚心锁ツ 2021-01-05 09:32

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

10条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 10:06

    There's probably a more elegant solution, but this works:

    func splitedString(string: String, length: Int) -> [String] {
        let string = Array(string.characters)
        let firstGroupLength = string.count % length
        var result: [String] = []
        var group = ""
    
        if firstGroupLength > 0 {
            for i in 0..

提交回复
热议问题