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

# replaceCharacters(in:with:)

Replaces the characters from `range` with those in `aString`.

```
func replaceCharacters(in range: NSRange, with aString: String)
```

## Parameters

`range`

The range of characters to replace. `range` must not exceed the bounds of the receiver.
  
  > Important:
  > Raises an `NSRangeException` if any part of `range` lies beyond the end of the receiver.

`aString`

The string with which to replace the characters in `range`. `aString` must not be `nil`.

## Discussion

This method treats the length of the string as a valid range value that returns an empty string.

---

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)