如何在数组中查找字符串元素而不考虑大写或小写
How to find string element in array regardless of upper or lower case
如果 myString
出现在 myArray
中,我想显示警报:
var myString = "A"
var myArray = [A]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
但是如果 myString
是相反的情况,则不会显示警报 :(
var myStr = "As"
var myArr = [AS]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
如何在 myArray
中忽略大小写找到 myString
?
我猜你要找的是类似下面的东西:
let search = "b"
let arrayToSearch = ["A", "B", "C"]
let searchResult = arrayToSearch.filter() { [=10=].caseInsensitiveCompare(search) == NSComparisonResult.OrderedSame }
if searchResult.count != 0 {
print("'\(search)' matched to '\(searchResult[0])'")
} else {
print("'\(search)'not found")
}
将输出
'b' matched to 'B'
如果 myString
出现在 myArray
中,我想显示警报:
var myString = "A"
var myArray = [A]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
但是如果 myString
是相反的情况,则不会显示警报 :(
var myStr = "As"
var myArr = [AS]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
如何在 myArray
中忽略大小写找到 myString
?
我猜你要找的是类似下面的东西:
let search = "b"
let arrayToSearch = ["A", "B", "C"]
let searchResult = arrayToSearch.filter() { [=10=].caseInsensitiveCompare(search) == NSComparisonResult.OrderedSame }
if searchResult.count != 0 {
print("'\(search)' matched to '\(searchResult[0])'")
} else {
print("'\(search)'not found")
}
将输出
'b' matched to 'B'