iOS SwiftUI-1 字串處理


Posted by John on 2020-12-30

1.字串分割
2.字串擷取
3.檢查字串
4.判斷前與後字串
5.大小寫轉換
6.搜尋字串,在...之前、在...之後的字符
7.搜尋字串範圍
8.字串串接
9.指定位置插入字符
10.字串範圍修改
11.刪除範圍內的字串,與字首、字尾
12.字串翻轉
13.字串遍歷
14.字串過濾
15.reduce用法

  1. 字串分割

    //分割字串
     let stringSplit = self.path.split(separator: "/")
     //如果數量是0,則存入"",否則存入最後一筆
     let string = stringSplit.count == 0 ? "" : String(format: String(stringSplit[stringSplit.count-1]))
    
  2. 字串擷取

     let str = "20200101170520.MP4"
     let index = str.index(str.startIndex, offsetBy: 5)
     let str01 = str[..<index]//20200
     let str02 = str[...index]//202001
     let str03 = str[index...]//101170520.MP4
    
     let path = "20200101170520.MP4"
     //設定index範圍,path.endIndex由右側開始,檢查長度大於4則以-4,否則不偏移,不做此判斷可能導致閃退
     let index = path.index(path.endIndex, offsetBy:  path.count > 4 ? -4 : 0)
    
     //prefix由左側開始擷取到index,upTo是指不包含-4那個位置
     //path_removeExtension = 20200101170520
     let path_removeExtension = String(path.prefix(upTo: index))
    
     //prefix由左側開始擷取到index,upTo是指包含-4那個位置
     //path_removeExtension = 20200101170520.
     let path_removeExtension = String(path.prefix(through: index))
    
     //prefix由左側開始擷取到5
     //path_removeExtension = 20200
     let path_removeExtension = String(path.prefix(5))
    
     //設定index範圍,path.startIndex由左側開始,檢查長度大於4則以4,否則不偏移,不做此判斷可能導致閃退
     let index = path.index(path.startIndex, offsetBy: path.count > 4 ? 4 : 0)
    
     //prefix由右側開始擷取到index,包含4那個位置
     //path_removeExtension = 0101170520.MP4
     let path_removeExtension = String(path.suffix(from: index))
    
     //prefix由右側開始擷取5個
     //path_removeExtension = 0.MP4
     let path_removeExtension = String(path.suffix(5))
    
  3. 檢查字串
    要判斷是否是空可以使用
     let str = "123"
     var isEmptyFlag = false
     if str.isEmpty {
         //未成立
         isEmptyFlag = true
     }
     //isEmptyFlag = false 判斷不為空
    
    判斷字串長度
     let str = "sssss"
     print("str.count \(str.count)") //str.count = 5
    
  4. 判斷前與後字串
     let str = "Hoooooo Prrrrr"
     var hasPrefix = false
     if str.hasPrefix("Hoooooo") {
         hasPrefix = true
     }
     //hasPrefix = true,在字首找到對應的字串
    
     let str = "Hoooooo Prrrrr"
     var hasSuffix = false
     if str.hasSuffix("Prrrrr") {
         hasSuffix = true
     }
     //hasSuffix = true,在字尾找到對應的字串
    
  5. 大小寫轉換
     let str = "HooPPOOO Prrrrr"
     str.uppercased()//全部大小
     str.lowercased()//全部小寫
     str.capitalized//字首大寫
    
  6. 搜尋字串,在...之前、在...之後的字符
     let str = "20200101170520.MP4"
     let str01 = str[str.index(after: str.startIndex)]//0
     let str02 = str[str.index(before: str.endIndex)]//4
    
  7. 搜尋字串範圍
     let str = "20234101170520.MP4"
     let str01 = str.range(of: "202341")
     //如果有搜尋到str01不為nil,且可以取得lowerBound與upperBound
    
  8. 字串串接
     var str = "20234101170520.MP4"
     str.append("Video")
    
  9. 指定位置插入字符

     var str = "20234101170520.MP4"
     let str01 = str.range(of: "10117")
     if let upper = str01?.upperBound {
         //插入單一字符
         str.insert("P", at: upper)//2023410117P0520.MP4
     }
     str = "20234101170520.MP4"
     if let lower = str01?.lowerBound {
         //插入單一字符
         str.insert("P", at: lower)//20234P101170520.MP4
     }
    
     var str = "20234101170520.MP4"
     let str01 = str.range(of: "10117")
     if let upper = str01?.upperBound {
         //插入多個字符
         str.insert(contentsOf: "Pa", at: upper)
     }
     str = "20234101170520.MP4"
     if let lower = str01?.lowerBound {
         //插入多個字符
         str.insert(contentsOf: "Pa", at: lower)
     }
    
  10. 字串範圍修改

    var str = "20234101170520.MP4"
    str.replaceSubrange(str.startIndex..<str.index(str.startIndex, offsetBy: 4), with: "Apple")
    //Apple4101170520.MP4
    
  11. 刪除範圍內的字串,與字首、字尾

    var str = "20234101170520.MP4"
    str.removeSubrange(str.startIndex..<str.index(str.startIndex, offsetBy: 4))
    //4101170520.MP4
    
    var str = "20234101170520.MP4"
    //刪除字首
    let str01 = str.dropFirst()//0234101170520.MP4
    //刪除兩個字首
    let str02 = str.dropFirst(2)//234101170520.MP4
    //刪除字尾
    let str03 = str.dropLast()//20234101170520.MP
    //刪除兩個字尾
    let str04 = str.dropLast(2)//20234101170520.M
    
  12. 字串翻轉

    var str = "20234101170520.MP4"
    let str01 = String(str.reversed())//4PM.02507110143202
    
  13. 字串遍歷

    //方法1
    var str = "20234101170520.MP4"
    for i in str {
        print(i)
    }
    //方法2
    str.forEach { char in
        print(char)
    }
    
    //方法3
    str.map { char in
        print("char \(char)")
    }
    
  14. 字串過濾

    var str = "20234101170520.MP4"
    
    let str01 = str.filter {
        $0 == "2" || $0 == "0"
    }
    //2020020
    
  15. reduce用法

    var str = "20234101170520.MP4"
    
    let str01 = str.reduce("Array", {(resulut,char) -> String in
        print("\(resulut + String(char))")
        /*
        Array2
        Array20
        Array202
        Array2023
        Array20234
        Array202341
        Array2023410
        Array20234101
        Array202341011
        Array2023410117
        Array20234101170
        Array202341011705
        Array2023410117052
        Array20234101170520
        Array20234101170520.
        Array20234101170520.M
        Array20234101170520.MP
        Array20234101170520.MP4
        */
        return resulut + String(char)
    })
    //str01最後是 Array20234101170520.MP4
    

#iOS #swift #XCode #Playground #string







Related Posts

[ 筆記 ] JavaScript 進階 06 - Closure

[ 筆記 ] JavaScript 進階 06 - Closure

[17] 原生功能 Natives - 內部特性、封裝、解封裝

[17] 原生功能 Natives - 內部特性、封裝、解封裝

如何做出一個好的 NodeJS 模組?

如何做出一個好的 NodeJS 模組?


Comments