# Issue with Insert function

**URL:** <https://forums.powershell.org/t/issue-with-insert-function/15088>\
**Category:** PowerShell Help\
**Created:** [October 14, 2020, 7:21am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088 "2020-10-14T07:21:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave11](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dave11/32/49_2.png) [@dave11](https://forums.powershell.org/u/dave11)\
**Post date:** [October 14, 2020, 7:21am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/1 "2020-10-14T07:21:48Z")

</div>

Hi,

I am having hard time using the insert function in powershell inside a loop. Currently I am trying to Insert string to existing string at particular index values. Here is code for the same.

```
$data = "admin"
$count = 0
for($i=0; $i -lt $data.Length -1 ; $i++ ){
    $data = $data.Insert($i,"XX")
    Write-Host "HERE"
}

Write-Host $data
```

But it goes into infinite-loop and prints “HERE” constantly. My end goal is to add string to existing string at particular index with some condition( But for now I am trying to achieve this to all characters)

Any suggestion/approach to achieve this are appriciated

Thanks

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 14, 2020, 8:29am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/2 "2020-10-14T08:29:58Z")

</div>

Your problem is due to the fact that the length is increasing with each interation so $i will always be less than $data.length -1 so it loops forever.

You need to calculate the final length of the string and stop there, you also need to increment by the number of characters in your string to be inserted rather than increment by 1 ($i++).

Something like this:

```
$data = 'admin'

$insertString = 'XX'

$finalLength = $data.Length + ($insertString.Length * $data.Length -1)

for ($i = 1; $i -lt $finalLength; $i = $i + $insertString.Length + 1 ) {

    $data = $data.Insert($i,$insertString)

}

Write-Host $data
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [October 14, 2020, 10:04am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/3 "2020-10-14T10:04:51Z")

</div>

Why are we doing a loop at all? If you are

> trying to Insert string to existing string at particular index values

 what part of that suggests a loop?

```
"existing string".Insert(8," i was inserted")

existing i was inserted string
```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 14, 2020, 10:22am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/4 "2020-10-14T10:22:49Z")

</div>

> [@](#):
>
> Why are we doing a loop at all?

Because OP said [quote quote=263331]My end goal is to add string to existing string at particular index with some condition( **But for now I am trying to achieve this to all characters** )[/quote]

My interpretation of that together with his original code was that he wanted to insert the string after each character.

---

<div class="post-metadata">

**Author:** ![dave11](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dave11/32/49_2.png) [@dave11](https://forums.powershell.org/u/dave11)\
**Post date:** [October 16, 2020, 7:14pm UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/5 "2020-10-16T19:14:33Z")

</div>

Hello [Matt Bloomfield](https://powershell.org/profile/Matt-Bloomfield/),

Sorry for late reply. You were right, I need to increase the total length to stop infinite loop. That solved the issue. Do you know by any chance, `Insert` function can throw any errors? because I tried playing around it but there were no exceptions on it.

Thanks

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 17, 2020, 1:24am UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/6 "2020-10-17T01:24:43Z")

</div>

According to [the documentation](https://docs.microsoft.com/en-us/dotnet/api/system.string.insert?view=netcore-3.1) it throws two exceptions:

**ArgumentNullException**  
value is null.

**ArgumentOutOfRangeException**  
startIndex is negative or greater than the length of this instance.

```
try { 
    'hello'.Insert()  
}

catch {
    $error[0].Exception
}

try { 
    'hello'.Insert(6,' world')  
}

catch {
    $error[0].Exception
}
```

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/issue-with-insert-function/15088/7 "2024-05-16T20:28:42Z")

</div>


