Welcome › Forums › General PowerShell Q&A › Issue with Insert function
- This topic has 5 replies, 3 voices, and was last updated 3 months ago by
Participant.
-
AuthorPosts
-
-
October 14, 2020 at 12:51 pm #263331
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.
PowerShell12345678$data = "admin"$count = 0for($i=0; $i -lt $data.Length -1 ; $i++ ){$data = $data.Insert($i,"XX")Write-Host "HERE"}Write-Host $dataBut 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
-
This topic was modified 3 months, 1 week ago by
grokkit. Reason: code formatting - please read the guide
-
This topic was modified 3 months, 1 week ago by
-
October 14, 2020 at 1:59 pm #263343
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:
PowerShell12345678910111213$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 -
October 14, 2020 at 3:34 pm #263382
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?
PowerShell123"existing string".Insert(8," i was inserted")existing i was inserted string -
October 14, 2020 at 3:52 pm #263391
Why are we doing a loop at all?
Because OP said
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)
My interpretation of that together with his original code was that he wanted to insert the string after each character.
-
October 17, 2020 at 12:44 am #264068
Hello 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
-
October 17, 2020 at 6:54 am #264122
According to the documentation it throws two exceptions:
ArgumentNullException
value is null.ArgumentOutOfRangeException
startIndex is negative or greater than the length of this instance.PowerShell123456789101112131415try {'hello'.Insert()}catch {$error[0].Exception}try {'hello'.Insert(6,' world')}catch {$error[0].Exception}
-
-
AuthorPosts
- The topic ‘Issue with Insert function’ is closed to new replies.