Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two strings ignoring case in Swift language?

Tags:

string

ios

swift

How can we compare two strings in swift ignoring case ? for eg :

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

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

like image 802
ak_tyagi Avatar asked May 29 '15 14:05

ak_tyagi


People also ask

How do you compare two strings to ignore the case?

The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

Which function is used to compare 2 strings by ignoring the case?

The equalsIgnoreCase() Method is used to compare a specified String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

How do you check if two strings are the same in Swift?

To check if two strings are equal in Swift, use equal to operator == with the two strings as operands. The equal to operator returns true if both the strings are equal or false if the strings are not equal.

How do you compare the first n characters of two strings by ignoring the case?

tf = strncmpi( s1,s2 , n ) compares up to n characters of s1 and s2 , ignoring any differences in letter case. The function returns 1 ( true ) if the two are identical and 0 ( false ) otherwise.


1 Answers

Try this :

For older swift:

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

Swift 3+

var a : String = "Cash" var b : String = "cash"      if(a.caseInsensitiveCompare(b) == .orderedSame){     print("Et voila") } 
like image 82
iAnurag Avatar answered Sep 20 '22 18:09

iAnurag