- prefix 方法
let str = "Hello, World!"
let subStr = str.prefix(5) // "Hello"
- suffix 方法
let str = "Hello, World!"
let subStr = str.suffix(6) // "World!"
- dropFirst 和 dropLast 方法
let str = "Hello, World!"
let subStr = str.dropFirst(7).dropLast(1) // "World"
- range(of:) 方法
let str = "Hello, World!"
if let range = str.range(of: "World") {
let subStr = str.substring(with: range) // "World"
}
- startIndex 和 endIndex 方法
let str = "Hello, World!"
let startIndex = str.index(str.startIndex, offsetBy: 7)
let endIndex = str.index(str.endIndex, offsetBy: -1)
let subStr = String(str[startIndex..<endIndex]) // "World"