1. 定义一个字符串数组:
var rowArraylist: [String] = ["aaa","bbb","18575:5:18589","ccc"]
2.将字符串中的第三列按照起始数值[18575~18589]按照步径step:5拆分为多列数据
in :字符串数组,要拆分所在列的Index. Out:二维数组
func splitOneLineIntoMultipleLinesByColon(rowArray: [String], valueIndex: Int) -> [[String]] {
var rowArrArr: [[String]] = [[]]
var newRowArray = rowArray
let valueArray = rowArray[valueIndex].split(separator: ":")
let min = Int(valueArray[0])!
let unit = Int(valueArray[1])!
let max = Int(valueArray[2])!
let distance = max - min
var addNumberOfLine = distance / unit
let addNumberOfLines = distance % unit
if addNumberOfLines != 0 {
addNumberOfLine = addNumberOfLine + 1
}
for i in 0...addNumberOfLine {
if i == addNumberOfLine {
newRowArray[valueIndex] = String(max)
}else {
newRowArray[valueIndex] = String(i * unit + min)
}
rowArrArr.append(newRowArray)
}
rowArrArr = Array(rowArrArr[rowArrArr.index(after: rowArrArr.startIndex)...])
return rowArrArr
}
3.初始化一个二维的字符串数组
var rowArraylists: [[String]] = [[]]
rowArraylists = splitOneLineIntoMultipleLinesByColon(rowArray: rowArraylist, valueIndex: 2)
for in循环打印每一行的数据
for (_, rowArray) in rowArraylists.enumerated() {
print(rowArray)
}
输出:
["aaa", "bbb", "18575", "ccc"]
["aaa", "bbb", "18580", "ccc"]
["aaa", "bbb", "18585", "ccc"]
["aaa", "bbb", "18589", "ccc"]
4.将table表中的某一列数据按照“,”拆分为多行数据
1)定义一个字符串数组
var rowArraylist1: [String] = ["aaa","bbb","0,1,2,3","ccc"]
2)实现方法:将table表中的某一列数据按照“,”拆分为多行数据
func splitOneLineIntoMultipleLinesByComma(rowArray: [String], valueIndex: Int) -> [[String]] {
var rowArrArr: [[String]] = [[]]
var newRowArray = rowArray
let valueArray = rowArray[valueIndex].split(separator: ",")
for newValue in valueArray {
newRowArray[valueIndex] = String(newValue)
rowArrArr.append(newRowArray)
}
rowArrArr = Array(rowArrArr[rowArrArr.index(after: rowArrArr.startIndex)...])
return rowArrArr
}
3)定义二维数组调用函数实现数据拆分,并循环打印结果。
var rowArraylists1: [[String]] = [[]]
rowArraylists1 = splitOneLineIntoMultipleLinesByComma(rowArray: rowArraylist1, valueIndex: 2)
for (_, rowArray) in rowArraylists1.enumerated() {
print(rowArray)
}
4)输出结果
["aaa", "bbb", "0", "ccc"]
["aaa", "bbb", "1", "ccc"]
["aaa", "bbb", "2", "ccc"]
["aaa", "bbb", "3", "ccc"]