字符串索引
let greeting = "swift"
greeting[greeting.startIndex]
greeting[greeting.index(before:greeting.endIndex)]
greeting[greeting.index(after:greeting.startIndex)]
let index = greeting.index(greeting.startIndex,offsetBy:3)
greeting[index]
插入与删除
let greeting = "swift"
greeting[greeting.index(before: greeting.endIndex)]
var greet = "swift"
greet.insert("!", at: greet.endIndex)
greet.insert(contentsOf: " to you ", at: greet.index(before: greet.endIndex))
let a = greet.remove(at: greet.index(before: greet.endIndex))
let range = greet.index(greet.endIndex, offsetBy: -3)..<greet.endIndex
greet.removeSubrange(range)
Substring
图片.png
let greeting = "Hello, world!"
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello"
// Convert the result to a String for long-term storage.
let newString = String(beginning)
网友评论