---
title: "commonPrefix(with:options:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsstring/commonprefix(with:options:)"
---

# commonPrefix(with:options:)

Returns a string containing characters the receiver and a given string have in common, starting from the beginning of each up to the first characters that aren’t equivalent.

## Declaration

```swift
func commonPrefix(with str: String, options mask: NSString.CompareOptions = []) -> String
```

## Parameters

- `str`: The string with which to compare the receiver.
- `mask`: Options for the comparison. The following search options may be specified by combining them with the C bitwise OR operator: NSCaseInsensitiveSearch, NSLiteralSearch. 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 A string containing characters the receiver and aString have in common, starting from the beginning of each up to the first characters that aren’t equivalent.

## Discussion

Discussion The returned string is based on the characters of the receiver. For example, if the receiver is “Ma¨dchen” and aString is “Mädchenschule”, the string returned is “Ma¨dchen”, not “Mädchen”.

## See Also

### Related Documentation

- [hasPrefix(_:)](foundation/nsstring/hasprefix(_:).md)
