Different ways to compare string in Swift

Alexey Karimov

Comparing strings is one of the most common tasks in Swift. Whether you’re validating user input, sorting lists, or matching values, choosing the right approach can help avoid subtle bugs — especially when dealing with different languages, cases, or accents.

This guide walks through how to compare strings in Swift accurately and efficiently, covering basic comparisons, case-insensitive checks, diacritic handling, and locale-aware sorting.

Basic Equality Comparison 

The most straightforward way to compare two strings is using the equality operator ==. This checks if two strings contain the exact same sequence of characters, including case and order.

let userInput = "Hello"
let storedValue = "Hello"
let otherValue = "World"

print(userInput == storedValue) // true
print(userInput == otherValue)  // false

You can also check for inequality using !=:

print(userInput != otherValue)  // true

Comparison with Lexicographic Order

Swift allows lexicographic comparison of strings (<, >, <=, >=), which is based on Unicode values:


let stringA = "Apple"
let stringB = "Banana"

print(stringA < stringB)  // true
print(stringA > stringB)  // false

Note: This is useful when sorting strings in a non-locale-sensitive way, such as internal identifiers or codes.

Case-Insensitive Comparison

If you want to compare strings without caring about letter casing, you can use:

lowercased(): Not Always Reliable

let a = "Fußball"
let b = "FUSSBALL"

print(a.lowercased() == b.lowercased()) // false (unexpected!)

In German, ß doesn’t lowercase to ss, so relying on lowercased() or uppercased() may fail with non-English content.

compare(_:options:): Highly Preferrable

Swift’s Foundation framework offers a much more accurate solution with the .caseInsensitive option:

let a = "Fußball"
let b = "FUSSBALL"

if a.compare(b, options: .caseInsensitive) == .orderedSame {
    print("Equal ignoring case")
}

This method handles Unicode and localization rules correctly and avoids the performance hit of creating new string instances with .lowercased().

Diacritic-Insensitive Comparison

To compare strings while ignoring accents (diacritics), use the .diacriticInsensitive option:

let plainE = "e"
let accentedE = "é"

if plainE.compare(accentedE, options: .diacriticInsensitive) == .orderedSame {
    print("Equal ignoring accents")
}

Locale-Aware Comparison

When showing sorted lists (like names, files, or cities) to users, you should respect their regional language rules. Use the .localizedStandardCompare(_:) method

let files = ["File10.txt", "file2.txt", "FILE1.txt"]

let sortedFiles = files.sorted {
    $0.localizedStandardCompare($1) == .orderedAscending
}

print(sortedFiles) // ["FILE1.txt", "file2.txt", "File10.txt"]

Which Swift String Comparison Method Should You Use?

Swift offers several ways to compare strings, each suited to different needs. If you’re running low on time, use this table to quickly find the most appropriate method based on your swift compare strings needs.

Use Case Recommended Method
Exact match==
Check inequality!=
Case-insensitive match.compare(_:options:) with .caseInsensitive
Ignore accents and diacritics.compare(_:options:) with .diacriticInsensitive
Case + accent insensitive.compare(_:options:) with both options
Locale-aware display sorting.localizedStandardCompare(_:)
Unicode-based lexicographic order<, >, <=, >=