Issue with Insert function

Welcome Forums General PowerShell Q&A Issue with Insert function

Viewing 5 reply threads
  • Author
    Posts
    • #263331
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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.

      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

      • This topic was modified 1 week, 5 days ago by grokkit. Reason: code formatting - please read the guide
    • #263343
      Participant
      Topics: 1
      Replies: 342
      Points: 326
      Helping Hand
      Rank: Contributor

      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:

       

    • #263382
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      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?

    • #263391
      Participant
      Topics: 1
      Replies: 342
      Points: 326
      Helping Hand
      Rank: Contributor

      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.

    • #264068
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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

    • #264122
      Participant
      Topics: 1
      Replies: 342
      Points: 326
      Helping Hand
      Rank: Contributor

      According to the documentation it throws two exceptions:

      ArgumentNullException
      value is null.

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

       

       

       

Viewing 5 reply threads
  • You must be logged in to reply to this topic.