---
title: "replaceCharacters(in:with:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nstext/replacecharacters(in:with:)"
---

# replaceCharacters(in:with:)

Replaces the characters in the given range with those in the given string.

## Declaration

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

## Parameters

- `range`: The range of characters to be replaced.
- `string`: The replacement string.

## Discussion

Discussion For a rich text object, the text of aString is assigned the formatting attributes of the first character of the text it replaces, or of the character immediately before aRange if the range’s length is 0. If the range’s location is 0, the formatting attributes of the first character in the receiver are used. This method does not include undo support by default. Clients must invoke shouldChangeText(inRanges:replacementStrings:) or shouldChangeText(in:replacementString:) to include this method in an undoable action. In most cases, programmatic modification of the text is best done by operating on the text storage directly, using the general methods of NSMutableAttributedString.

## See Also

### Replacing text

- [replaceCharacters(in:withRTF:)](appkit/nstext/replacecharacters(in:withrtf:).md)
- [replaceCharacters(in:withRTFD:)](appkit/nstext/replacecharacters(in:withrtfd:).md)
