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

# range(of:options:range:locale:)

Finds and returns the range of the first occurrence of a given string within a given range of the string, subject to given options, using the specified locale, if any.

```
func range(of searchString: String, options mask: NSString.CompareOptions = [], range rangeOfReceiverToSearch: NSRange, locale: Locale?) -> NSRange
```

## Parameters

`searchString`

The string for which to search.

`mask`

A mask specifying search options. The following options may be specified by combining them with the C bitwise `OR` operator: `NSCaseInsensitiveSearch`, `NSLiteralSearch`, `NSBackwardsSearch`, and `NSAnchoredSearch`. 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.

`rangeOfReceiverToSearch`

The range within the receiver for which to search for `aString`.

    Raises an [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException)     if     `aRange`     is invalid.

`locale`

The locale to use when comparing the receiver with `aString`. To use the current locale, pass [[`NSLocale`](/documentation/Foundation/NSLocale) [`current`](/documentation/Foundation/NSLocale/current)]. To use the system locale, pass `nil`.

    The locale argument affects the equality checking algorithm. For example, for the Turkish locale, case-insensitive compare matches “I” to “ı” (    `U+0131 LATIN SMALL DOTLESS I`    ), not the normal “i” character.

## Return Value

An [`NSRange`](/documentation/Foundation/NSRange-c.struct) structure giving the location and length in the receiver of `aString` within `aRange` in the receiver, modulo the options in `mask`. The range returned is relative to the start of the string, not to the passed-in range. Returns `{``NSNotFound``, 0}` if `aString` is not found or is empty (`""`).

## Discussion

`NSString` objects are compared by checking the Unicode canonical equivalence of their code point sequences.  The length of the returned range and that of `aString` may differ if equivalent composed character sequences are matched.

> Important:
> When working with text that’s presented to the user, use the ``doc://com.apple.foundation/documentation/Foundation/NSString/localizedStandardRange(of:)`` method instead.

### Special Considerations

This method detects all invalid ranges (including those with negative lengths). For applications linked against macOS 10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution.

---

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)