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
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.
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.
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.
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.
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") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With