How to compare two strings ignoring case in Swift?

How can we compare two lines in random ignoring? eg:

var a = "Cash" var b = "cash" 

Is there any method that will return true if we compare var a and var b

+80
string ios swift
May 29 '15 at
source share
16 answers

Try this:

For older Swift:

 var a : String = "Cash" var b : String = "cash" if(a.caseInsensitiveCompare(b) == NSComparisonResult.OrderedSame){ println("voila") } 



Swift 3.0 and 4.1

  var a : String = "Cash" var b : String = "cash" if(a.caseInsensitiveCompare(b) == .orderedSame){ print("voila") } 
+164
May 29 '15 at
source share

Use the caseInsensitiveCompare method:

 let a = "Cash" let b = "cash" let c = a.caseInsensitiveCompare(b) == .orderedSame print(c) // "true" 

ComparisonResult tells you which word occurs earlier than the other in lexicographical order (i.e., which word is closer to the beginning of the dictionary). .orderedSame means that .orderedSame strings in one place in the dictionary

+32
May 29 '15 at 15:05
source share
 if a.lowercaseString == b.lowercaseString { //Strings match } 
+21
May 29 '15 at 15:00
source share

Try the following:

 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 - enumeration type NSComparisonResult:

 enum NSComparisonResult : Int { case OrderedAscending case OrderedSame case OrderedDescending } 

So you can use the if statement:

 if result == .OrderedSame { println("equal") } else { println("not equal") } 
+8
May 29 '15 at 14:57
source share

It can just collapse its:

 func equalIgnoringCase(a:String, b:String) -> Bool { return a.lowercaseString == b.lowercaseString } 
+4
May 29 '15 at 2:59
source share

Swift 3

THE RIGHT WAY:

  let a: String = "Cash" let b: String = "cash" if a.caseInsensitiveCompare(b) == .orderedSame { //Strings match } 

Note: ComparisonResult.orderedSame can also be written as .orderedSame in abbreviated form.

OTHER WAYS:

but.

  if a.lowercased() == b.lowercased() { //Strings match } 

b.

  if a.uppercased() == b.uppercased() { //Strings match } 

from.

  if a.capitalized() == b.capitalized() { //Strings match } 
+4
May 08 '17 at 17:03
source share

You can also make all the letters in upper case (or lower case) and see if they match.

 var a = "Cash" var b = "CASh" if a.uppercaseString == b.uppercaseString{ //DO SOMETHING } 

This will make both variables "CASH" and therefore they are equal.

You can also do extension String

 extension String{ func equalsIgnoreCase(string:String) -> Bool{ return self.uppercaseString == string.uppercaseString } } if "Something ELSE".equalsIgnoreCase("something Else"){ print("TRUE") } 
+1
May 29 '15 at 15:01
source share

localizedCaseInsensitiveContains : returns whether the recipient contains the specified string by performing case-sensitive and locale-sensitive searches.

 if a.localizedCaseInsensitiveContains(b) { //returns true if a contains b (case insensitive) } 

Edited by :

caseInsensitiveCompare : returns the result of a comparison call (_: options :) with NSCaseInsensitiveSearch as the only option.

 if a.caseInsensitiveCompare(b) == .orderedSame { //returns true if a equals b (case insensitive) } 
+1
Mar 05 '18 at 5:02
source share

An example of comparing phone numbers; using swift 4.2

  var selectPhone = [String]() if selectPhone.index(where: {$0.caseInsensitiveCompare(contactsList[indexPath.row].phone!) == .orderedSame}) != nil { print("Same value") } else { print("Not the same") } 
+1
02 Oct '18 at 18:02
source share
 extension String { func equalIgnoreCase(_ compare:String) -> Bool { return self.uppercased() == compare.uppercased() } } 

usage pattern

  print("lala".equalIgnoreCase("LALA")) print("l4la".equalIgnoreCase("LALA")) print("laLa".equalIgnoreCase("LALA")) print("LALa".equalIgnoreCase("LALA")) 
0
Dec 22 '17 at 6:58
source share

Swift 4, I took the String extension path, using caseInsensitiveCompare () as a template (but letting the operand be optional). Here's a playground I used to put it together (new to Swift, so reviews are more than welcome)

 import UIKit extension String { func caseInsensitiveEquals<T>(_ otherString: T?) -> Bool where T : StringProtocol { guard let otherString = otherString else { return false } return self.caseInsensitiveCompare(otherString) == ComparisonResult.orderedSame } } "string 1".caseInsensitiveEquals("string 2") // false "thingy".caseInsensitiveEquals("thingy") // true let nilString1: String? = nil "woohoo".caseInsensitiveEquals(nilString1) // false 
0
Jan 23 '18 at 0:01
source share

You can simply write your String Extension to compare in just a few lines of code

 extension String { func compare(_ with : String)->Bool{ return self.caseInsensitiveCompare(with) == .orderedSame } } 
0
May 08 '19 at 6:41
source share

For Swift 5, ignore case and compare two lines

 var a = "cash" var b = "Cash" if(a.caseInsensitiveCompare(b) == .orderedSame){ print("Ok") } 
0
Jun 15 '19 at 2:24
source share

Swift 3

 if a.lowercased() == b.lowercased() { } 
-one
Nov 15 '16 at 10:16
source share

Swift 3 . You can define your own operator, for example. ~= .

 infix operator ~= func ~=(lhs: String, rhs: String) -> Bool { return lhs.caseInsensitiveCompare(rhs) == .orderedSame } 

What can you try on the playground

 let low = "hej" let up = "Hej" func test() { if low ~= up { print("same") } else { print("not same") } } test() // prints 'same' 
-one
Dec 02 '16 at 17:35
source share

Swift 3:

You can also use a localized case-insensitive comparison between two string functions and returns Bool

 var a = "cash" var b = "Cash" if a.localizedCaseInsensitiveContains(b) { print("Identical") } else { print("Non Identical") } 
-3
May 08 '17 at 16:38
source share



All Articles