在Swift 3中,我想从字符串数组创建一个匹配字符串数组(不区分大小写):
我正在使用这段代码,但它区分大小写,
let filteredArray = self.arrCountry.filter { $0.contains("india") }
我怎么能做到这一点..假设我有一个名为arrCountry的主字符串数组,我想创建包含“印度”(不区分大小写)的所有字符串的其他数组。
有人能帮帮我吗。
发布于 2016-10-03 20:25:22
您可以尝试使用localizedCaseInsensitiveContains
let filteredArray = self.arrCountry.filter { $0.localizedCaseInsensitiveContains("india") }
发布于 2020-02-10 12:13:06
答案2020:
即使我输入中文或阿拉伯语,下面的测试代码仍然返回TRUE
let text = "total sdfs"
let text1 = "Total 张"
let text2 = "TOTAL لطيف"
let text3 = "total :"
let text4 = "ToTaL : "
text.lowercased().contains("total")
text1.lowercased().contains("张")
text2.lowercased().contains("لطيف")
text3.lowercased().contains("total")
text4.lowercased().contains("total")
text.localizedCaseInsensitiveContains("total")
text1.localizedCaseInsensitiveContains("张")
text2.localizedCaseInsensitiveContains("لطيف")
text3.localizedCaseInsensitiveContains("total")
text4.localizedCaseInsensitiveContains("total")
发布于 2021-05-14 14:58:19
我在使用过滤时的2美分(例如,对于SwiftUI中的列表。如果您正在过滤,则空字符串返回false:
let filterString = ""
let s = "HELLO"
let f = s.localizedCaseInsensitiveContains(filterString)
print(f)
举个例子:
……
private let people = ["Finn", "Leia", "Luke", "Rey"]
func getList(filterString: String)->[String]{
if filterString.isEmpty{
return people
}
let result = people.filter({ (p: String) -> Bool in
print(p)
let f = p.localizedCaseInsensitiveContains(filterString)
return f
})
return result
}
https://stackoverflow.com/questions/39831448
复制相似问题