检查数组中是否存在字符串不敏感

时间:2015-07-09 22:45:58

标签: swift contains

声明:

let listArray = ["kashif"]
let word = "kashif"

然后这个

contains(listArray, word) 

返回true但如果声明为:

let word = "Kashif"

然后返回false,因为比较区分大小写。

如何使这个比较案例不敏感?

11 个答案:

答案 0 :(得分:38)

<% @one.each do |c| %>
<option data-id=<%=c[0]%> style="color:<%=c[2]%>" value=<%=c[1]%>><%=c[1]%></option>
<% end %>

Xcode 7.3.1•Swift 2.2.1

let list = ["kashif"]
let word = "Kashif"

if contains(list, {$0.caseInsensitiveCompare(word) == NSComparisonResult.OrderedSame}) {
    println(true)  // true
}

Xcode 8•Swift 3或更高版本

if list.contains({$0.caseInsensitiveCompare(word) == .OrderedSame}) {
    print(true)  // true
}

或者:

if list.contains(where: {$0.caseInsensitiveCompare(word) == .orderedSame}) {
    print(true)  // true
}

如果你想知道数组中元素的位置(它可能找到多个与谓词匹配的元素):

if list.contains(where: {$0.compare(word, options: .caseInsensitive) == .orderedSame}) {
    print(true)  // true
}

答案 1 :(得分:22)

你可以使用

word.lowercaseString 

将字符串转换为全部小写字符

答案 2 :(得分:9)

要检查数组中是否存在字符串(不区分大小写),请使用

listArray.localizedCaseInsensitiveContainsString(word) 

其中listArray是数组的名称 和word是你的搜索文本

此代码适用于Swift 2.2

答案 3 :(得分:7)

试试这个:

let loword = word.lowercaseString
let found = contains(listArray) { $0.lowercaseString == loword }

答案 4 :(得分:3)

Swift 4

只是让所有内容(查询和结果)都不区分大小写。

for item in listArray {
    if item.lowercased().contains(word.lowercased()) {
        searchResults.append(item)
    }
}

答案 5 :(得分:3)

swift 5,swift 4.2,请使用下面的代码。

let list = ["kAshif"]
let word = "Kashif"

if list.contains(where: { $0.caseInsensitiveCompare(word) == .orderedSame }) {
    print("contains is true")
}

答案 6 :(得分:3)

您可以添加扩展名:

雨燕5

extension Array where Element == String {
    func containsIgnoringCase(_ element: Element) -> Bool {
        contains { $0.caseInsensitiveCompare(element) == .orderedSame }
    }
}

并像这样使用它:

["tEst"].containsIgnoringCase("TeSt") // true

答案 7 :(得分:1)

SWIFT 3.0:

在字符串数组中查找不区分大小写的字符串很酷,但是如果你没有索引,那么在某些情况下它就不会很酷。

这是我的解决方案:

let stringArray = ["FOO", "bar"]()
if let index = stringArray.index(where: {$0.caseInsensitiveCompare("foo") == .orderedSame}) {
   print("STRING \(stringArray[index]) FOUND AT INDEX \(index)")
   //prints "STRING FOO FOUND AT INDEX 0"                                             
}

这比其他答案更好b / c你有数组中对象的索引,所以你可以抓住对象并做任何你想做的事情:)

答案 8 :(得分:1)

用于检查字符串是否存在于具有更多选项(不区分大小写,锚定/搜索仅限于开始)的数组中

使用Foundation range(of:options:)

let list = ["kashif"]
let word = "Kashif"


if list.contains(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
    print(true)  // true
}

if let index = list.index(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
    print("Found at index \(index)")  // true
}

答案 9 :(得分:1)

扩展@Govind Kumawat的答案

searchString中将word进行的简单比较是:

word.range(of: searchString, options: .caseInsensitive) != nil

作为功能:

func containsCaseInsensitive(searchString: String, in string: String) -> Bool {
    return string.range(of: searchString, options: .caseInsensitive) != nil
}

func containsCaseInsensitive(searchString: String, in array: [String]) -> Bool {
    return array.contains {$0.range(of: searchString, options: .caseInsensitive) != nil}
}

func caseInsensitiveMatches(searchString: String, in array: [String]) -> [String] {
    return array.compactMap { string in
        return string.range(of: searchString, options: .caseInsensitive) != nil
            ? string
            : nil
    }
}

答案 10 :(得分:0)

我的例子

func updateSearchResultsForSearchController(searchController: UISearchController) {
    guard let searchText = searchController.searchBar.text else { return }
    let countries = Countries.getAllCountries()
    filteredCountries = countries.filter() {
        return $0.name.containsString(searchText) || $0.name.lowercaseString.containsString(searchText)
    }
    self.tableView.reloadData()
}
相关问题