Trimming question

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Arie H Arie H 5 months, 1 week ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #37996
    Profile photo of taotao07
    taotao07
    Participant

    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
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant
    $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
    Profile photo of taotao07
    taotao07
    Participant

    Whoa, that was fast! Thank you Sir!

    #37999
    Profile photo of Arie H
    Arie H
    Participant

    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

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.