gpt4 book ai didi

swift - 一个变量中存在多个 RangeOfString

转载 作者:行者123 更新时间:2023-11-30 12:54:04 31 4
gpt4 key购买 nike

有什么解决方案可以让我像下面的代码一样吗?

let tmp: NSString = data.status
let tmp1: NSString = data.process
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) && tmp1.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound

我这样做是因为我想搜索多个字段。谢谢

最佳答案

如果您正在寻找范围,则应该使用RegularExpression

import Foundation

let pattern = "search"

let regexp = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options())
let string = "String to search in search"
regexp.enumerateMatches(in: string, options: NSRegularExpression.MatchingOptions(), range: NSMakeRange(0, string.characters.count), using: { (result, flags, stop) in

if result?.range != nil {
print("Range is \(result!.range.location)-\(result!.range.length)")
}
})

关于swift - 一个变量中存在多个 RangeOfString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40627661/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com