---
title: "replaceSubrange(_:with:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/string/replacesubrange(_:with:)"
---

# replaceSubrange(_:with:)

Replaces the text within the specified bounds with the given characters.

## Declaration

```swift
mutating func replaceSubrange<C>(_ subrange: Range<String.Index>, with newElements: C) where C : Collection, C.Element == Character
```

## Parameters

- `subrange`: The range of text to replace. The bounds of the range must be valid indices of the string.
- `newElements`: The new characters to add to the string.

## Discussion

Discussion Calling this method invalidates any existing indices for use with this string. note: O(m), where m is the combined length of the string and newElements. If the call to replaceSubrange(_:with:) simply removes text at the end of the string, the complexity is O(n), where n is equal to bounds.count.

## See Also

### Replacing Substrings

- [replaceSubrange(_:with:)](swift/string/replacesubrange(_:with:)-72947.md)
