Different ways to compare string in Swift

⋅ 3 min read ⋅ Swift String

Table of Contents

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

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

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()

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 é
}

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Read more article about Swift, String, or see all available topic

Enjoy the read?

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. No strings attached. Unsubscribe anytime.

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.

Become a patron Buy me a coffee Tweet Share
Previous
TextField in SwiftUI

How to create and use TextField in SwiftUI.

Next
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.

← Home