<!--
{
  "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/NSMutableAttributedString/replaceCharacters(in:with:)-1uaw7",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableAttributedString(im)replaceCharactersInRange:withAttributedString:"
  },
  "title" : "replaceCharacters(in:with:)"
}
-->

# replaceCharacters(in:with:)

Replaces the characters and attributes in a given range with the characters and attributes of the given attributed string.

```
func replaceCharacters(in range: NSRange, with attrString: NSAttributedString)
```

## Parameters

`range`

The range of characters and attributes replaced.

`attrString`

The attributed string whose characters and attributes replace those in the specified range.

## Discussion

Raises an [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException) if any part of `range` lies beyond the end of the receiver’s characters.

---

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)