let string1 = "hello"
// 获取某个下标后一个下标对应的字符
var char = string1[string1.index(after: string1.startIndex)]
print(char)
// 获取某个下标前一个下标对饮的字符
var char2 = string1[string1.index(before: string1.endIndex)]
print(char2)
// 通过范围获取字符串中的一个子串 Hello
var subString = string1[string1.startIndex...string1.index(string1.startIndex, offsetBy: 4)]
print(subString)
var subString2 = string1[string1.index(string1.endIndex, offsetBy: -5)..<string1.endIndex]
print(subString2)
// 获取某个子串在父串中的范围
var string = "Hello-Swift"
var range = string.range(of: "Hello")
//print(range)
// 追加一个字符
string.append(Character("!"))
// 在指定位置插入一个字符
string.insert("~", at: string.index(string.startIndex, offsetBy: 5))
// 在指定位置插入一组字符
string.insert(contentsOf: ["~","~","~"], at: string.index(string.startIndex, offsetBy: 5))
// 在指定范围替换一个字符串
string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
// 在指定位置删除一个字符
string.remove(at: string.index(before: string.endIndex))
// 删除指定范围的字符
string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
// 删除所有字符
string.removeAll()
// 字符串大小写转化
var string2 = "My name is Jaki"
string2 = string2.uppercased()
string2 = string2.lowercased()
// 检查字符串是否有My前缀
string2.hasPrefix("My")
string2.hasSuffix("Jaki")
|