Swift 字符串 数组常见操作及相互转换

1. Array 常见操作

  • 增删改查
var tt = ["A","B"]
tt.append("C")  //tt = A,B,C
tt.remove(at: 0) //tt = B,C
tt[1] = "D"      //tt = B,D
tt.insert("Y", at: 1) //tt = B,Y,D

let index = tt.firstIndex(of: "D")
print(index) //1
  • 获取指定位置数据 或 获取指定数据下标
let nums = ["a","b","c","d","b","c","d","c","e","f"] 
let first = nums.first //数组第一个元素
let anyObj = nums[2]   //对应下标
let last = nums.last //数组最后一个元素
    
print(first!,"--", anyObj ,"--",last!)
//a -- c -- f

// 获取第一个“b”的下标
let firstIndex = nums.firstIndex(of: "b")
let lastIndex = nums.lastIndex(of: "b")
print(firstIndex!,"--",lastIndex!)
//1 -- 4


// 获取第一个不小于“d”的下标
let index1 = nums.firstIndex { (str) -> Bool in
     return str < "d"
 }
 print("\(index1!)") // 0

 // 获取最后一个不小于“d”的下标
 let index2 = nums.lastIndex { (str) -> Bool in
     return str < "d"
 }
 print("\(index2!)")// 7
  • 数组的拼接
var tt1 = ["A","A"]
let tt2 = ["a","b"]

let ta1 = tt1 + tt2
let ta2 = [tt1,tt2].joined()
tt1 += tt2

print(ta1)//["A", "A", "a", "b"]
print(ta2)
print(Array(ta2))
print(tt1)

["A", "A", "a", "b"]
FlattenSequence>>(_base: [["A", "A"], ["a", "b"]])
["A", "A", "a", "b"]
["A", "A", "a", "b"]


  • 数组的截取
    print("=======截取======")
    let nums = ["a","b","c","d","b","c","d","c","e","f"] 
    // prefix 从数组0到2(< 2)
    let pre1 = nums.prefix(upTo: 2)
    print(pre1)
    // 传入一个长度值 等同于 上面
    let pre2 = nums.prefix(2)
    print(pre2)
    // prefix 从数组0到2(<= 2)
    let pre3 = nums.prefix(through: 2)
    print(pre3)

    print("=======后缀======")
    // suffix 后缀
    // suffix 下标为3开始到最后
    let suf1 = nums.suffix(from: 3)
    print(suf1)
    
    // 传入一个长度值
    let suf2 = nums.suffix(3)
    print(suf2)

=======前缀======
["a", "b"]
["a", "b"]
["a", "b", "c"]
=======后缀======
["d", "b", "c", "d", "c", "e", "f"]
["c", "e", "f"]

  • 数组的筛选
class Dog {
    var name: String = ""
    var age : Int = 0
}

var dogs: [Dog?]? = nil
筛选出年龄大于等于3岁的狗狗
  • 方式一:
// 筛选出age>=3的狗狗
let filterDogs = tempModels.filter { (model) -> Bool in
       model?.age >= 3
}
  • 方式二:
   var fliterDogs = [Dog?]()
   if let oriDogs = dogs {
        for dog in oriDogs where dog?.age >= 3{
             fliterDogs.append(dog)
        }
   }

2. String 操作字符串类比数组操作

  • 增删改查 、截取 等 可对比数组操作
  • 字符串拼接
        let y  = "2020"
        let m = "10"
        let d  = "01"
        let totalStr = y + m + d // 20201001
        let totalStr2 = "\(y)-\(m)-\(d)"//2020-10-01
        ...
  • 字符串替换
let dateStr = "2020-10-01"
let subStr = dateStr.replacingOccurrences(of: "-", with: "") 
// 替换后为: 20201001

ps: 也可用这种方式去除字符串中空格


3. 数组字符串相互转换

  • 李明:18211232321/财务主管 中分离出电话号,并拼接成 电话:18211232321
    let msg = "李明:18211232321/财务主管"
    //李明:18211232321  ,   财务主管
    let msgArr = msg.components(separatedBy: "/")
    //18211232321
    let telNum = msgArr[0].suffix(11)
    
    let aStr = "电话:"
    let bStr = aStr + telNum
    print(bStr)//电话:18211232321
  • 服务器返回1,2,31、2、3分别对应 主管、组长 、 组员,则移动端应显示的文本内容
    let keysStr  = "1,2,3"
    let keysArr = keysStr.components(separatedBy: ",")
    var valuesArr = [String]()
    keysArr.forEach { (key) in
        if key == "1"{
            valuesArr.append("主管")
        }else if key == "2"{
            valuesArr.append("组长")
        }else{
            valuesArr.append("组员")
        }
    }
    let valueStr = valuesArr.joined(separator: ",")
    print(valueStr)//主管,组长,组员

你可能感兴趣的:(Swift 字符串 数组常见操作及相互转换)