美文网首页
swift字符串

swift字符串

作者: 古草犹香 | 来源:发表于2019-10-28 16:10 被阅读0次

cgImage 图片时间"2019:10:25 12:30:24" 转"2019-10-25 12:30" 或"2019.10.25 12:30"

// dateTimeOriginal  2019:10:25 12:30:24
let arr = dateTimeOriginal?.components(separatedBy: " ")
var dateStr = arr?[0]
let timeStr = arr?[1]
                    
dateStr= dateStr?.replacingOccurrences(of: ":", with: ".")
                    
let index = timeStr!.index(timeStr!.endIndex, offsetBy: -3)
let suffix = timeStr![..<index]
let dataTimeStr = dateStr + " " + timeStr  // 2019.10.25 12:30

playground上运行演示:

import Foundation

let s = " / 2 3 4 ? / "

// 替换 => -/-2-3-4-?-/-
print("空格替换成-:", s.replacingOccurrences(of: " ", with: "-"))
//eg: 2019:10:27 => 2019-10-27  or 2019.10.27

// 过滤 => /234?/
print("空格过滤掉:", s.replacingOccurrences(of: " ", with: ""))

// 去首尾空格 => / 2 3 4 ? /
print("去掉空格:", s.trimmingCharacters(in: .whitespaces))

// 分割 => [" ", " 2 3 4 ? ", " "]
print("分割:", s.components(separatedBy: "/"))

// 拼接 => 1-2-3
let a = ["1", "2", "3"]
print("拼接:", a.joined(separator: "-"))

let timeString = "2016.12.12 08:30:40"
let timeIndex = timeString.index(timeString.endIndex, offsetBy: -3)
//let result = timeString.substring(from: index) // deprecated
let timeResult = timeString[..<timeIndex]

print(timeResult) //  2016.12.12 08:30

let addressStr = "地址:上海南京西路888号!!!!"
let addressStartIndex = addressStr.index(addressStr.startIndex, offsetBy: 3)
var addressResult = addressStr[addressStartIndex...]
print(addressResult) // 上海南京西路888号!!!!

let addressEndIndex = addressStr.index(addressStr.endIndex, offsetBy: -4)
addressResult = addressStr[addressStartIndex...addressEndIndex]
print(addressResult) // 上海南京西路888号!
print(addressStr[addressStartIndex..<addressEndIndex]) // 上海南京西路888号

// 视频元数据处理
// +31.2379+121.4702+040.492/
//2019-10-26T22:05:42+0800

let videoMetaGPS = "+31.2379+121.4702+040.492/"
let metaGPSArr = videoMetaGPS.components(separatedBy: "+")
print(metaGPSArr)   // ["", "31.2379", "121.4702", "040.492/"]
let videoMetaDate = "2019-10-26T22:05:42+0800"
let metaDateArr = videoMetaDate.components(separatedBy: "T")
print(metaDateArr)  // ["2019-10-26", "22:05:42+0800"]
let year = metaDateArr[0]
let timeStr = metaDateArr[1]
print(timeStr[..<timeStr.index(timeStr.startIndex, offsetBy: 5)])  // 22:05

相关文章

网友评论

      本文标题:swift字符串

      本文链接:https://www.haomeiwen.com/subject/yjubvctx.html