将字符串数组转换为连接字符串数组

时间:2017-09-26 19:27:42

标签: ios arrays swift swift3

我正在寻找一种通用的功能解决方案,将数组的x元素连接到一个新的字符串数组。从第一个元素开始,首先是第二个元素,第二个元素是第三个元素,等等......对不起,解释不是很清楚,但是我很难在我想要实现的目标上加上名字。所以我将尝试用示例来演示它。

1)如果我试图连接字母2乘2我使用此功能:

    var letters = ["a","b","c","d","e"]

   func concatenateStrings(array: [String]) -> [String] {
        return array.reduce([], { (result: [String], item:String) -> [String] in
            guard !result.isEmpty else {
                return [item] //first item
            }

            return result + [ array[result.count - 1] + " \(item)"]
        })
    }

生成了这个结果["a", "a b", "b c", "c d", "d e"]

2)3乘3

  func concatenateStrings(array: [String]) -> [String] {
        return array.reduce([], { (result: [String], item:String) -> [String] in
            guard !result.isEmpty else {
                return [item] //first item
            }
            guard result.count != 1 else {
                return result + [array[result.count - 1] + " \(item)"] // second item
            }
            let first = array[result.count - 2]
            let second = array[result.count - 1]
            return result + [ "\(first) " + second + " \(item)"]
        })
    }

给了我["a", "a b", "a b c", "b c d", "c d e"]

我的问题是我如何概括这个方法,例如我想通过连接4个元素,5个数组元素等来生成一个新的字符串数组......

谢谢,如果还有不清楚的地方,我会很乐意尝试更加彻底。

2 个答案:

答案 0 :(得分:4)

可能的解决方案(Swift 3 + 4):

func concatenateStrings(array: [String], maxLength: Int) -> [String] {

    let nestedArray = array.reduce([[String]]()) { (result, item) -> [[String]] in
        let last = result.last ?? []
        return result + [Array((last + [item]).suffix(maxLength))]
    }
    return nestedArray.map { $0.joined(separator: " ") }
}

示例:

let letters = ["a","b","c","d","e"]
print(concatenateStrings(array: letters, maxLength: 3))
// ["a", "a b", "a b c", "b c d", "c d e"]

该函数首先创建一个嵌套的字符串数组,例如

[["a"], ["a", "b"], ["a", "b", "c"], ["b", "c", "d"], ["c", "d", "e"]]

将当前项追加到最后一个元素,然后使用 suffix()限制长度。然后连接内部数组。

在Swift 4中,您还可以使用reduce(into:)来创建更少的内容 中间数组,请参阅SE-0171 Reduce with inout

func concatenateStrings(array: [String], maxLength: Int) -> [String] {

    let nestedArray = array.reduce(into: [[String]]()) { (result, item) in
        let last = result.last ?? []
        result.append(Array((last + [item]).suffix(maxLength)))
    }
    return nestedArray.map { $0.joined(separator: " ") }
}

答案 1 :(得分:1)

鉴于年轻人输入数组

let elms = ["a","b","c","d","e"]

这是一个实现你需要的迭代函数

  

Swift 4版

func concatenations(elms:[String], maxLength: Int) -> [String] {

    var result:[String] = []

    var word = ""

    for elm in elms {
        if word.count >= maxLength {
            word.removeFirst()
            word.append(elm)
        } else {
            word.append(elm)
        }
        result.append(word)
    }
    return result
}

实施例

concatenations(elms: elms, maxLenght: 2)
// ["a", "ab", "bc", "cd", "de"]


concatenations(elms: elms, maxLenght: 3)
// ["a", "ab", "abc", "bcd", "cde"]


concatenations(elms: elms, maxLenght: 4)
// ["a", "ab", "abc", "abcd", "bcde"]