Trimming question

Welcome Forums General PowerShell Q&A Trimming question

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #37996

    Participant
    Points: 0
    Rank: Member

    Hi there, so i have this variable that has the following data:
    Region1|BU1|Site1
    Region1|BU1|Site2
    Region1|BU2|Site3
    Region1|BU2|Site4
    Region1|BU2|Site5
    Region1|BU2|Site6
    Region1|BU2|Site7
    Region1|BU2|Site8
    Region1|BU3|Site9
    Region1|BU4|Site10
    Region1|BU4|Site11
    Region1|BU4|Site12
    What I would like to do is to trim the last part, which is the |Site# – I've tried few things and i just cannot get it right, I can do foreach and then do split and re-assemble it again only with the first two parts, but i'm thinking (since everything's possible in powershell 🙂 ) there's gotta be a better way.
    At the end I just need Region1|BU# – i don't mind if they repeat, i can do unique and that will take care if it.
    Thanks in advance!

  • #37997

    Participant
    Points: 0
    Rank: Member
    $a = @(
    'Region1|BU1|Site1'
    'Region1|BU1|Site2'
    'Region1|BU2|Site3'
    'Region1|BU2|Site4'
    'Region1|BU2|Site5'
    'Region1|BU2|Site6'
    'Region1|BU2|Site7'
    'Region1|BU2|Site8'
    'Region1|BU3|Site9'
    'Region1|BU4|Site10'
    'Region1|BU4|Site11'
    'Region1|BU4|Site12'
    )
    
    $a -replace '\|[^\|]+$'
    
  • #37998

    Participant
    Points: 0
    Rank: Member

    Whoa, that was fast! Thank you Sir!

  • #37999

    Participant
    Points: -19
    Rank: Member

    Will the length of the data be the same ? 6-1-3 ?
    If so use Substring(0,9)

    If the size isn't always the same, youll have to use LastIndexOf("|") and then
    use substring but deduct 1 from the value of LastIndexOf("|")

    can always try RegEx but dont have to go that path for every single string manipulation

The topic ‘Trimming question’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort