Results 1 to 3 of 3
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,015
    Thanks
    429
    Thanked 1,611 Times in 1,460 Posts

    A little PowerShell Gotchya!

    Hey Y'all,

    Here's a little anomaly, or lack of understanding on my part, in the way the Trim function works in PowerShell.

    Code:
    #Setup:
    PS> $x = '\Microsoft\Office\Office Automatic Updates'
    PS> $p = '\Microsoft\Office\'
    
    #Test Trim Start
    PS> $x.TrimStart("$p")
     Automatic Updates
    
    #Note how the TrimStart command not only trims the value of $P out of $X but also kills the 'Office' from 'Office Automatic Updates'!
    
    #It gets even worse if you use just Trim:
    PS> $x.Trim("$p")
     Automatic Upda
    
    #Conclusion the Trim functions will get rid of any part of the specified value on the beginning or end of the trimmed string up to the first non recognized character.
    
    #To actually get what I was after I had to do this:
    
    PS> $x.Substring($p.Length,$x.Length-$p.length)
    Office Automatic Updates
    Thus, Test, Test, and Test again!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    24,372
    Thanks
    5
    Thanked 1,213 Times in 1,058 Posts
    TrimStart works on an array of characters built from the "trim" string. It terminates the trim when a character not in the array is found.

    See the Remarks section at String.TrimStart Method for more information on how TrimStart works.
    Joe

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,015
    Thanks
    429
    Thanked 1,611 Times in 1,460 Posts
    Joe,

    The key is "array of Characters". It's a list of individual characters not words or phrases which is where I was misinterpreting it.

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •