Different ways to compare string in Swift
source link: https://sarunw.com/posts/different-ways-to-compare-string-in-swift/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Different ways to compare string in Swift
String comparison is an essential operation for day to day job. Swift provides a few variations for this. We will visit them in this article.
Test for string equality in Swift
In Swift, you can check for string and character equality with the "equal to" operator (==
) and "not equal to" operator (!=
).
let password = "123456"
let passwordConfirmation = "123456"
password == passwordConfirmation
// true
Learn everything you need to know about Sign in with Apple to be able to integrate it in your existing app or a new one.
Test for character equality in Swift
We use the same operator for Character
.
let a: Character = "a"
let alsoA: Character = "a"
a == alsoA
// true
These "equal to" operator is suitable for simple string comparison where you required an exact match. If you more control over the matching criteria, such as ignore case or diacritic marks, you will need the help of compare
.
Compare two strings ignoring case in Swift
To make a case insensitive comparison, we use compare(_:options:)
.
let a = "a"
let capitalA = "A"
a.compare(capitalA, options: .caseInsensitive)
// ComparisonResult.orderedSame
if a.compare(capitalA, options: .caseInsensitive) == .orderedSame {
// a is equals to A
}
You can alos use a shortform of caseInsensitiveCompare(_:)
.
let a = "a"
let capitalA = "A"
a.caseInsensitiveCompare(capitalA)
// ComparisonResult.orderedSame
Caveat
You might be tempted to use lowercased()
and uppercased()
with the "equal to" operator to do case insensitive comparison.
let a = "a"
let capitalA = "A"
a.lowercased() == capitalA.lowercased()
// true
a.uppercased() == capitalA.uppercased()
// true
You should get an expected result most of the time, but that's not always the case.
The only problem I know so far is the word "Straße", which means street in German.
let street = "Straße"
let alsoStreet = "STRASSE"
print(street.lowercased())
// straße
print(alsoStreet.uppercased())
// STRASSE
street.lowercased() == alsoStreet.lowercased()
// false
street.uppercased() == alsoStreet.uppercased()
// true
street.compare(alsoStreet, options: .caseInsensitive) == .orderedSame
// true
The problem is "ß" is lowercased, which doesn't get any conversion with lowercased()
, but got convert to SS with uppercased()
. This kind of special treatment in language might cause you an unexpected behavior. This is only one example from one language, but we can't know for sure if this can happen elsewhere, so I suggest you use compare
for this kind of job.
I also think that street.compare(alsoStreet, options: .caseInsensitive) == .orderedSame
sending a clearer message for the reader.
This would convey a message that you want to compare
two strings ignoring their cases (.caseInsensitive
).
street.compare(alsoStreet, options: .caseInsensitive) == .orderedSame
While this means the lowercase form of two strings should be equals. Very different, right?
street.lowercased() == alsoStreet.lowercased()
I might be wrong about the meaning and assumption of the word "Straße" because I don't know German. My point is you can't assume that uppercase or lowercase form in other languages would be as simple and straightforward as English.
You can read more here: What is the appropriate capitalization of 'ß'?
Compare two strings ignoring diacritic marks in Swift
A diacritic mark is a glyph added to a letter or basic glyph, which use in some languages.
To compare strings ignoring diacritic, we also use compare(_:options:)
, but this time we pass .diacriticInsensitive
as an option.
let e = "e"
let eWithAcuteAccent = "é"
e.compare(eWithAcuteAccent, options: .diacriticInsensitive)
// ComparisonResult.orderedSame
if e.compare(eWithAcuteAccent, options: .diacriticInsensitive) == .orderedSame {
// e is equals to é
}
Learn everything you need to know about Sign in with Apple to be able to integrate it in your existing app or a new one.
Related Resources
Get new posts weekly
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Different ways to check if a string contains another string in Swift
Learn how to check if a string contains another string, numbers, uppercased/lowercased string, or special characters.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK