Instance Method

compare:options:range:locale:

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

Declaration

- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale;

Parameters

aString

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 for details on these options.

range

The range of the receiver over which to perform the comparison. The range must not exceed the bounds of the receiver.

locale

An instance of NSLocale. To use the current locale, pass [NSLocale currentLocale]. 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 NSComparisonResult value that indicates the lexical ordering of a specified range within the receiver and a given string. NSOrderedAscending if the substring of the receiver given by range precedes aString in lexical ordering for the locale given in dict, NSOrderedSame if the substring of the receiver and aString are equivalent in lexical value, and NSOrderedDescending 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”.

See Also

Identifying and Comparing Strings

- caseInsensitiveCompare:

Returns the result of invoking compare:options: with NSCaseInsensitiveSearch as the only option.

- localizedCaseInsensitiveCompare:

Compares the string with a given string using a case-insensitive, localized, comparison.

- compare:

Returns the result of invoking compare:options:range: with no options and the receiver’s full extent as the range.

- localizedCompare:

Compares the string and a given string using a localized comparison.

- compare:options:

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

- compare:options:range:

Returns the result of invoking compare:options:range:locale: with a nil locale.

- localizedStandardCompare:

Compares strings as sorted by the Finder.

- hasPrefix:

Returns a Boolean value that indicates whether a given string matches the beginning characters of the receiver.

- hasSuffix:

Returns a Boolean value that indicates whether a given string matches the ending characters of the receiver.

- isEqualToString:

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

hash

An unsigned integer that can be used as a hash table address.

NSStringCompareOptions

These values represent the options available to many of the string classes’ search and comparison methods.

NSStringEncodingConversionOptions

Options for converting string encodings.