<!--
{
  "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/insert(contentsOf:at:)-rdu9",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SmsE6insert10contentsOf2atyqd__n_5IndexQztSlRd__7ElementQyd__AFRtzlF::SYNTHESIZED::s:SS"
  },
  "title" : "insert(contentsOf:at:)"
}
-->

# insert(contentsOf:at:)

Inserts the elements of a sequence into the collection at the specified
position.

```
mutating func insert<C>(contentsOf newElements: C, at i: Self.Index) where C : Collection, Self.Element == C.Element
```

## Parameters

`newElements`

The new elements to insert into the collection.

`i`

The position at which to insert the new elements. `index`
must be a valid index of the collection.

## Discussion

The new elements are inserted before the element currently at the
specified index. If you pass the collection’s `endIndex` property as the
`index` parameter, the new elements are appended to the collection.

Here’s an example of inserting a range of integers into an array of the
same type:

```
var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints "[1, 2, 3, 100, 101, 102, 103, 4, 5]"
```

Calling this method may invalidate any existing indices for use with this
collection.

> Complexity: O(*n* + *m*), where *n* is length of this collection and
> *m* is the length of `newElements`. If `i == endIndex`, this method
> is equivalent to `append(contentsOf:)`.

---

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)