Replacing specific numbers in list of numbers

Welcome Forums General PowerShell Q&A Replacing specific numbers in list of numbers

Viewing 4 reply threads
  • Author
    Posts
    • #271384
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      Hello All!

      I’m a bit new to powershell and I’m having trouble and need some guidance on how to accomplish something.

      I want to be able to remove specific numbers in a string of numbers.

      Say I have this: 608,8,154,266

      I’m passing this in and want to remove the 8 and 154 as well as the ,,

      Here’s my If:

      if (($list -eq “8”) -or ($list -eq “266”)){
      $newlist = $list -Replace “8”, “”
      $newlist = $newlist -Replace “266”, “”
      $newlist = $newlist -Replace “,,”, “,”
      $newlist = $newlist .TrimEnd(“,”)
      $newlist = $newlist .TrimStart(“,”)
      LogWrite “newlist : $newlist ”
      }

      I want my list to end up like 608,266 and not 60,266.

      I also tried using a trim but can’t seem to get it to work.

      Do you have any suggestions on how to help a newby out?

       

      Any guidance is greatly appreciated!

    • #271390
      Participant
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      Is this a single string i.e. “608,8,154,266” or an array of integers?  I’m not exactly sure what you are trying to accomplish but it sounds like it would be much easier to work with an array.  To do so just use the -split operator which will return an array of strings.  Then you can iterate over the array and do whatever you want.  Finally if you need to return a single string just use the -join operator to put it back together.

       

    • #271405
      Participant
      Topics: 17
      Replies: 1869
      Points: 3,631
      Helping Hand
      Rank: Community Hero

      It’s important to understand if it’s a string or an array. Mike shows you how to parse the string into an array and then parse. Mike is also converting it back to a string, but it would be best to explain what you are doing with the output:

    • #271420
      Participant
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      Just to piggy back on Rob’s comments.  In PowerShell a comma is an operator that instantiates an array so:

      is an array of integers.  If the comma is inside enclosed in quotations it is part of the string and not an array operator so:

      Is a single string and not an array.  The split operator will take a single string and create an array of strings.

      This creates an array of strings (even though they are numbers).  PowerShell will automatically convert the type if needed as long as there is an appropriate method i.e. toString(), toInt132().  For example you can do this is PowerShell:

      and the result will be $true because PS “knows” to convert the string 15 to an integer in order to do the comparison.  Be careful with relying on automatic conversions like this because some operators work differently depending on the type.  For example:

      You might think this would return 25 but the plus operator will concatenate strings, so PS converts the 10 to a string and performs concatenation instead of converting the “15” to an integer and performing addition.

       

      • This reply was modified 2 weeks, 6 days ago by Mike R..
    • #271555
      Participant
      Topics: 0
      Replies: 1
      Points: 7
      Rank: Member

      I’m glad I had made the right decision when ordered my first essay here on https://topessaybrands.com service. Not only do these guys take the load off your shoulders, but they also offer their work as a sample to learn from! Now I can pay a small amount of money and got my essay done in few days. The writer is very professional and put all her efforts into my literature subject.

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