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

# hasPrefix(_:)

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

```
func hasPrefix(_ str: String) -> Bool
```

## Parameters

`str`

A string.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if `aString` matches the beginning characters of the receiver, otherwise <doc://com.apple.documentation/documentation/Swift/false>. Returns <doc://com.apple.documentation/documentation/Swift/false> if `aString` is empty.

## Discussion

This method is a convenience for comparing strings using the `NSAnchoredSearch` option. See [String Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035i) for more information.

---

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)