---
title: "How do you compare strings in Swift?"
description: "Compare Swift strings with ==, caseInsensitiveCompare, localizedStandardCompare and prefix checks. Learn why Unicode equivalence and locale matter."
url: "https://bugsee.com/answers/swift/swift-compare-strings/"
---

# How do you compare strings in Swift?

Use `==` for equality, `caseInsensitiveCompare` or `localizedCaseInsensitiveCompare` to ignore case, and `localizedStandardCompare` to sort strings the way users expect.

```swift
let a = "Café"
let b = "Cafe\u{301}"        // e + combining accent

a == b                        // true: Swift compares canonical equivalence
"apple" < "banana"            // true: lexicographic order

"Hello".caseInsensitiveCompare("hello") == .orderedSame     // true
"Straße".localizedCaseInsensitiveCompare("STRASSE")          // locale-aware

"file10" .localizedStandardCompare("file2")   // .orderedDescending, numbers sorted naturally

"swiftui".hasPrefix("swift")   // true
"swiftui".contains("ui")       // true (Foundation, or the standard library on newer Swift)
```

Plain `==` and `<` are exact and fast, which suits identifiers and keys. For text shown to users, use the localized comparisons so accents, case and digits sort as people expect.

## Related questions

- [How do you convert a String to an Int in Swift?](https://bugsee.com/answers/swift/convert-a-string-to-an-int/)
