欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

swift 字符串部分操作

程序员文章站 2022-06-08 13:54:55
...
let ss = "<p>你a好b呀!</p>"
//替换处理
print("去掉a: \(ss.replacingOccurrences(of: "a",with: ""))")

print("去掉标签 \(ss.replacingOccurrences(of: "<p>",with: ""))")

print("去掉标签 \(ss.replacingOccurrences(of: "</p>",with: ""))")

print("去掉首尾空格: \(ss.trimmingCharacters(in: .whitespaces))")

let b = ["aa","bb","cc"]
print("字符串拼接 \(b.joined(separator: "-"))")


let dataString = "2019.03.28"
let index = dataString.index(dataString.endIndex, offsetBy: -2)
let suffix = dataString.substring(from: index)
let indexa = dataString.index(dataString.startIndex, offsetBy: 2)
let prefix = dataString.substring(to: indexa)
let startRange = dataString.index(dataString.startIndex, offsetBy: 5)
let endRange = dataString.index(dataString.startIndex, offsetBy: 7)
let middle = dataString.substring(with: Range(startRange..<endRange))

 

相关标签: swift