<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/compare(_:options:range:locale:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)compare:options:range:locale:"
  },
  "title" : "compare(_:options:range:locale:)"
}
-->

# compare(_:options:range:locale:)

Compares the string using the specified options and returns the lexical ordering for the range.

```
func compare(_ string: String, options mask: NSString.CompareOptions = [], range rangeOfReceiverToCompare: NSRange, locale: Any?) -> ComparisonResult
```

## Parameters

`string`

The string with which to compare the range of the receiver specified by `range`.

    This value must not be     `nil`    . If this value is     `nil`    , the behavior is undefined and may change in future versions of macOS.

`mask`

Options for the search—you can combine any of the following using a C bitwise OR operator: `NSCaseInsensitiveSearch`, `NSLiteralSearch`, `NSNumericSearch`.

    See     [String Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035i)     for details on these options.

`rangeOfReceiverToCompare`

The range of the receiver over which to perform the comparison. The range must not exceed the bounds of the receiver.
  
  > Important:
  > Raises an `NSRangeException` if `range` exceeds the bounds of the receiver.

`locale`

An instance of [`NSLocale`](/documentation/Foundation/NSLocale). To use the current locale, pass [[`NSLocale`](/documentation/Foundation/NSLocale)  [`current`](/documentation/Foundation/NSLocale/current)]. For example, if you are comparing strings to present to the end-user, use the current locale. To use the system locale, pass `nil`.

## Return Value

Returns an [`ComparisonResult`](/documentation/Foundation/ComparisonResult) value that indicates the lexical ordering of a specified range within the receiver and a given string. [`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending) if the substring of the receiver given by `range` precedes `aString` in lexical ordering for the locale given in `dict`, [`ComparisonResult.orderedSame`](/documentation/Foundation/ComparisonResult/orderedSame) if the substring of the receiver and `aString` are equivalent in lexical value, and [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending) if the substring of the receiver follows `aString`.

## Discussion

The `locale` argument affects both equality and ordering algorithms. For example, in some locales, accented characters are ordered immediately after the base; other locales order them after “z”.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)