Powershell – Trim

Welcome Forums General PowerShell Q&A Powershell – Trim

Viewing 4 reply threads
  • Author
    Posts
    • #269762
      Participant
      Topics: 4
      Replies: 5
      Points: 30
      Rank: Member

      Hi to all,

      I am trying to find out away how to trim a string from a specific character.

      let say I have a package name:  Baybon_v1.0

      How can I trim this to show only: Babylon

      Another example:

      Microsoft office professional_v2016

      Would like to trim the text starting “_”

      The result should be:

      Microsoft office professional

      Thank you so much for your time and help

      Amir 🙂

       

    • #269783
      Participant
      Topics: 2
      Replies: 5
      Points: 55
      Rank: Member

      This example is hardcoded but it works

      OUTPUT = Baybon

      OUTPUT = Microsoft office professional

       

      If you want this to be generic, such as function that works for all cases, consider wildcards or regexes in

      combination with String.Trim()

    • #269792
      Participant
      Topics: 1
      Replies: 55
      Points: 525
      Helping Hand
      Rank: Major Contributor

      <p style=”text-align: left;”>Another option would be to use Split. You can split string by specific character and create array.</p>
      On you case you will split by _ and will use the first element of that array.

      Hope that helps

    • #269795
      Participant
      Topics: 9
      Replies: 677
      Points: 2,680
      Helping Hand
      Rank: Community Hero

      Few more options

      • This reply was modified 3 weeks, 4 days ago by Doug Maurer.
    • #269873
      Participant
      Topics: 4
      Replies: 5
      Points: 30
      Rank: Member

      WOW… i never thought there are so may ways to do one complex task 🙂

      Thank you guys 🙂

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