<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/String/UnicodeScalarView/replaceSubrange(_:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SS17UnicodeScalarViewV15replaceSubrange_4withySnySS5IndexVG_xtSlRzs0A0O0B0V7ElementRtzlF"
  },
  "title" : "replaceSubrange(_:with:)"
}
-->

# replaceSubrange(_:with:)

Replaces the elements within the specified bounds with the given Unicode
scalar values.

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

## Parameters

`subrange`

The range of elements to replace. The bounds of the range
must be valid indices of the view.

`newElements`

The new Unicode scalar values to add to the string.

## Discussion

Calling this method invalidates any existing indices for use with this
string.

> Complexity: O(*m*), where *m* is the combined length of the view and
> `newElements`. If the call to `replaceSubrange(_:with:)` simply
> removes elements at the end of the string, the complexity is O(*n*),
> where *n* is equal to `bounds.count`.

---

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)