How to split array objects into two(or 2d array) by seperation character

后端 未结 3 1136
误落风尘
误落风尘 2021-01-24 22:05

I want to split an array like shown below.

let arrayToSplit = [\"Europe|#|France|#|Paris\", \"Europe|#|Italy|#|Rome\", \"America|#|USA|#|Washington\", \"America|         


        
3条回答
  •  猫巷女王i
    2021-01-24 22:38

    let arrayToSplit = ["Europe|#|France|#|Paris", "Europe|#|Italy|#|Rome", "America|#|USA|#|Washington", "America|#|Canada|#|Ottawa"]
    
    let arrayOfLists = arrayToSplit.map{
        $0.componentsSeparatedByString("|#|")
    }
    
    let columns = Array(0..<3).map{ i in
        arrayOfLists.map{ list in list[i]}
    }
    
    print(columns)
    //[["Europe", "Europe", "America", "America"], ["France", "Italy", "USA", "Canada"], ["Paris", "Rome", "Washington", "Ottawa"]]
    

提交回复
热议问题