Instance Method


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


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



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.


Options for the search—you can combine any of the following using a C bitwise OR operator: case​Insensitive, literal, numeric. See String Programming Guide for details on these options.

Return Value

Returns an Comparison​Result value that indicates the lexical ordering. ordered​Ascending the receiver precedes a​String in lexical ordering, ordered​Same the receiver and a​String are equivalent in lexical value, and ordered​Descending if the receiver follows a​String.


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.

See Also

Related Symbols

func is​Equal(to:​ String)

Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.

func localized​Standard​Compare(String)

Compares strings as sorted by the Finder.

func compare(String, options:​ NSString.Compare​Options = [], range:​ NSRange, locale:​ Any?)

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