租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

在Swift 3.0中使用String.range,swift 应用

发布时间:2023-09-27 08:58:39

在Swift 3.0中使用String.range

在Swift 3.0中,你可使用String.range来获得子字符串在原始字符串中的位置。这里是一个简单的示例:

let str = "Hello, World!"
let range = str.range(of: "World")
if let range = range {
let startIndex = str.distance(from: str.startIndex, to: range.lowerBound)
let endIndex = str.distance(from: str.startIndex, to: range.upperBound)
print("Found at index (startIndex) to (endIndex)")
} else {
print("Not found")
}

输出结果将是 "Found at index 7 to 12",表示子字符串 "World" 在原始字符串中的位置范围。