How to compare two strings in Swift

Updated Apr 27, 2024#swift#strings

Comparing strings in Swift is not as simple as it may seem. Some of the aspects that you may want to consider when comparing strings:

  • Case sensitivity: Do you want to treat uppercase and lowercase letters as the same or different? For example, "Apple" and "apple" are the same in a case-insensitive comparison, but different in a case-sensitive one.
  • Diacritic sensitivity: Do you want to ignore or respect the diacritic marks (such as accents or umlauts) that may appear in some languages? For example, "café" and "cafe" are the same in a diacritic-insensitive comparison, but different in a diacritic-sensitive one.
  • Locale sensitivity: Do you want to follow the rules and conventions of a specific language or region when comparing strings? For example, "ß" and "ss" are the same in German, but different in English.
  • Numeric sensitivity: Do you want to compare strings that contain numbers as if they were numeric values or as if they were text? For example, "2" and "02" are the same in a numeric-sensitive comparison, but different in a numeric-insensitive one.
  • Width sensitivity: Do you want to distinguish between characters that have the same shape but different widths, such as full-width and half-width characters in some Asian languages? For example, "A" and "A" are the same in a width-insensitive comparison, but different in a width-sensitive one.

You should also test your code with different inputs and edge cases to ensure that your comparisons are accurate and consistent.

  1. The simplest way to compare two strings is to use the == operator, which returns true if the strings are exactly the same, and false otherwise.
let a = "Hello"
let b = "Hello"
let c = "hello"

a == b // true
a == c // false
  1. However, sometimes you may want to compare two strings without considering the case or the diacritic marks (such as accents or umlauts). In that case, you can use the compare(_:options:) method, which takes a string and an option parameter, and returns a ComparisonResult value.
let a = "café"
let b = "Cafe"
let c = "Café"

a.compare(b, options: .caseInsensitive) // .orderedSame
a.compare(c, options: .diacriticInsensitive) // .orderedSame
a.compare(b, options: [.caseInsensitive, .diacriticInsensitive]) // .orderedSame

The compare(_:options:) method also allows you to specify other options, such as .numeric, .widthInsensitive, or .forcedOrdering.

When working with text that’s presented to the user, use the localizedStandardCompare(_:) instead, or use the compare(_:options:range:locale:) method, passing the user’s locale.

  1. Alternatively, you can use the caseInsensitiveCompare(_:) method, which is an instance method belonging to the String class. You call it on a string object to compare it with another string.
let a = "a"
let b = "A"

a.caseInsensitiveCompare(b) // .orderedSame

It works similar to compare(_:options:) with the .caseInsensitive option as both performs a case-insensitive comparison of two strings.

You can also use localizedCaseInsensitiveCompare(_:) if you need to compare two strings in a way that is case-insensitive and respects the current locale settings.