<!--
{
  "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:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)compare:options:"
  },
  "title" : "compare(_:options:)"
}
-->

# compare(_:options:)

Compares the string with the specified string using the given options.

```
func compare(_ string: String, options mask: NSString.CompareOptions = []) -> ComparisonResult
```

## Parameters

`string`

The string with which to compare the receiver.

    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: [`caseInsensitive`](/documentation/Foundation/NSString/CompareOptions/caseInsensitive), [`literal`](/documentation/Foundation/NSString/CompareOptions/literal), [`numeric`](/documentation/Foundation/NSString/CompareOptions/numeric). 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.

## Return Value

Returns an [`ComparisonResult`](/documentation/Foundation/ComparisonResult) value that indicates the lexical ordering. [`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending) the receiver precedes `aString` in lexical ordering, [`ComparisonResult.orderedSame`](/documentation/Foundation/ComparisonResult/orderedSame) the receiver and `aString` are equivalent in lexical value, and [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending) if the receiver follows `aString`.

## Discussion

This method is equivalent to invoking [`compare(_:options:range:)`](/documentation/Foundation/NSString/compare(_:options:range:)) with a given mask as the options and the receiver’s full extent as the range.

> Important:
> When working with text that’s presented to the user, use the ``doc://com.apple.foundation/documentation/Foundation/NSString/localizedStandardCompare(_:)`` instead, or use the ``doc://com.apple.foundation/documentation/Foundation/NSString/compare(_:options:range:locale:)`` method, passing the user’s locale.

---

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)