Case Insensitive Matching Search in String Array Swift 3

case insensitive matching search in string array swift 3

You can try with localizedCaseInsensitiveContains

let filteredArray = self.arrCountry.filter { $0.localizedCaseInsensitiveContains("india") }

Check if a string exists in an array case insensitively

you can use

word.lowercaseString 

to convert the string to all lowercase characters

Search string in ArrayAnyObject with case insensitive

"č" is the Unicode character "LATIN SMALL LETTER C WITH CARON".
It can be decomposed into "c" + "ˇ" where the latter (the "CARON") is a so-called "diacritical mark".

To ignore diacritical marks when comparing strings (so that
"č" = "c"), use the .DiacriticInsensitiveSearch option:

let list = ["č", "ž"]
let search = "C"

if (list.contains {
$0.compare(search, options: [.DiacriticInsensitiveSearch, .CaseInsensitiveSearch]) == .OrderedSame
}) {
print("Found")
}

Or, if you want to find matching substrings:

if (list.contains {
$0.rangeOfString(search, options: [.DiacriticInsensitiveSearch, .CaseInsensitiveSearch]) != nil
}) {
print("Found")
}

How can filter array without case sensitivity?

I tested this on a playground:

let searchText = "apple"
let dataArray = ["Apple", "Pear", "Banana", "Orange"]
let filteredArray = dataArray.filter{ $0.lowercased().hasPrefix(searchText.lowercased()) }
print(filteredArray)

You have to use hasPrefix to achieve what you want, and in order for the query to be case insensitive you can lowercase both your array and the query string.

Case incensitive filter searchable text in an array of strings

For strict equality, case insensitive:

return data.collectables.filter {
searchText.isEmpty
? true
: $0.tags.contains { tag in tag.caseInsensitiveCompare(searchText) == .orderedSame }
}

for substrings, you can use the range(of:) method:

return data.collectables.filter {
searchText.isEmpty
? true
: $0.tags.contains { tag in tag.range(of: searchText, options: .caseInsensitive) != nil }
}

or, use localizedCaseInsensitiveContains which you have already used before:

return data.collectables.filter {
searchText.isEmpty
? true
: $0.tags.contains { tag in tag.localizedCaseInsensitiveContains(searchText) }
}

Filter Dictionary with a case insensitive search

Contains method it is the same as range(of: "String") != nil without any options. All you need is to use range of String != nil with caseInsensitive options:

extension String {
func contains(_ string: String, options: CompareOptions) -> Bool {
return range(of: string, options: options) != nil
}
}

Now you can do:

"whatever".contains("ER", options: .caseInsensitive)

If you need to create a dictionary from your array of dictionaries, you would need to use forEach to iterate through the result and rebuild your dictionary from it:


let facilityDict: [Int: [String: String]] = [
17: ["id": "199", "facilitycode": "036", "location_name": "Centerpoint Medical Offices"],
41: ["id": "223", "facilitycode": "162", "location_name": "Dark Ridge Medical Center"],
14: ["id": "196", "facilitycode": "023", "location_name": "Spinnerpark"],
20: ["id": "202", "facilitycode": "048", "location_name": "Educational Theater"],
30: ["id": "212", "facilitycode": "090", "location_name": "Partner Medical Offices"],
49: ["id": "231", "facilitycode": "223", "location_name": "GreenBay Administrative Offices"]]

var filtered: [Int: [String: String]] = [:]

facilityDict.filter{$0.value.contains{$0.value.contains("AR", options: .caseInsensitive)}}.forEach{filtered[$0.key] = $0.value}

print(filtered) // [30: ["id": "212", "facilitycode": "090", "location_name": "Partner Medical Offices"], 41: ["id": "223", "facilitycode": "162", "location_name": "Dark Ridge Medical Center"], 14: ["id": "196", "facilitycode": "023", "location_name": "Spinnerpark"]]

How to compare two strings ignoring case in Swift language?

Try this:

var a = "Cash"
var b = "cash"
let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil)

// You can also ignore last two parameters(thanks 0x7fffffff)
//let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch)

result is type of NSComparisonResult enum:

enum NSComparisonResult : Int {

case OrderedAscending
case OrderedSame
case OrderedDescending
}

So you can use if statement:

if result == .OrderedSame {
println("equal")
} else {
println("not equal")
}

Matching string value associative enums in an array in a case insensitive way

You just need to define Equatable conformance yourself. And for the case of comparing two string cases, use string.caseInsensitiveCompare instead of the default synthesised implementation, which just uses == for the two String associated values.

enum Type {
case int(Int)
case string(String)
}

extension Type: Equatable {
static func ==(lhs: Type, rhs: Type) -> Bool {
switch (lhs, rhs) {
case (.int(let num), .int(let otherNum)):
return num == otherNum
case (.string(let string), .string(let otherString)):
return string.caseInsensitiveCompare(otherString) == .orderedSame
default:
return false
}
}
}


Related Topics



Leave a reply



Submit