---
title: "compare(_:options:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsstring/compare(_:options:)"
---

# compare(_:options:)

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

## Declaration

```swift
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: doc://com.apple.foundation/documentation/Foundation/NSString/CompareOptions/caseInsensitive, doc://com.apple.foundation/documentation/Foundation/NSString/CompareOptions/literal, doc://com.apple.foundation/documentation/Foundation/NSString/CompareOptions/numeric. See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035i for details on these options.

## Return Value

Return Value Returns an ComparisonResult value that indicates the lexical ordering. ComparisonResult.orderedAscending the receiver precedes aString in lexical ordering, ComparisonResult.orderedSame the receiver and aString are equivalent in lexical value, and ComparisonResult.orderedDescending if the receiver follows aString.

## Discussion

Discussion This method is equivalent to invoking 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 localizedStandardCompare(_:) instead, or use the compare(_:options:range:locale:) method, passing the user’s locale.

## See Also

### Identifying and Comparing Strings

- [caseInsensitiveCompare(_:)](foundation/nsstring/caseinsensitivecompare(_:).md)
- [localizedCaseInsensitiveCompare(_:)](foundation/nsstring/localizedcaseinsensitivecompare(_:).md)
- [compare(_:)](foundation/nsstring/compare(_:).md)
- [localizedCompare(_:)](foundation/nsstring/localizedcompare(_:).md)
- [compare(_:options:range:)](foundation/nsstring/compare(_:options:range:).md)
- [compare(_:options:range:locale:)](foundation/nsstring/compare(_:options:range:locale:).md)
- [localizedStandardCompare(_:)](foundation/nsstring/localizedstandardcompare(_:).md)
- [hasPrefix(_:)](foundation/nsstring/hasprefix(_:).md)
- [hasSuffix(_:)](foundation/nsstring/hassuffix(_:).md)
- [isEqual(to:)](foundation/nsstring/isequal(to:).md)
- [hash](foundation/nsstring/hash.md)
- [NSString.CompareOptions](foundation/nsstring/compareoptions.md)
- [NSString.EncodingConversionOptions](foundation/nsstring/encodingconversionoptions.md)
