1.删除指定位置的字符串,
let str = "abcdefg"
let str3 = str.substring(from: str.index(str.endIndex, offsetBy: -4))
print(str3)输出defg
let str4 = str.substring(to: str.index(str.startIndex, offsetBy: 4))
print(str4)输出abcd
2、插入字符串
var str = "abcdefg"
str.insert(contentsOf: "****".characters, at: str.index(str.startIndex, offsetBy: 3))
print(str)输出abc****defg
3、更换字符串
var str = "abcdefg"
let str2 = str.replacingOccurrences(of: "ab", with: "")
print(str2)输出cdefg
网友评论