Problems with trim()

Welcome Forums General PowerShell Q&A Problems with trim()

Viewing 4 reply threads
  • Author
    Posts
    • #176641
      Participant
      Topics: 4
      Replies: 6
      Points: 44
      Rank: Member

      Hi All,

      I recently realized I was having issues with a script because of extra white space.  I am calling the trim function but it's not doing anything.  Am I calling it wrong?

      $test = "user1@example.com                              "
      $test.Length
      $test.TrimEnd()
      $test.Length
      $test.Trim()
      $test.Length
      $test
      
      PS C:\temp> $test = "user1@example.com     "
      $test.Length
      $test.TrimEnd()
      $test.Length
      $test.Trim()
      $test.Length
      $test
      36
      user1@example.com
      36
      user1@example.com
      36
      user1@example.com
      
      PS C:\temp>

      -Chris

    • #176647
      Participant
      Topics: 1
      Replies: 20
      Points: 66
      Rank: Member

      Perhaps I am missing something, but you are not putting the new value that has been trimmed, back into the "test" variable.

      Example:

      
      PS C:\> $test = "user1@example.com              "
      PS C:\> $test.Length
      31
      PS C:\> $test = $test.Trim()
      PS C:\> $test.Length
      17
      
      
    • #176650
      Participant
      Topics: 1
      Replies: 1635
      Points: 3,089
      Helping Hand
      Rank: Community Hero

      I think it does not work as you expect it to. 😉 Let's take your first 4 lines of code ...

      $test = "user1@example.com                   "
      $test.Length
      $test = $test.TrimEnd()
      $test.Length

      If you want to make your change permanent to the variable you have to assign it.

    • #176653
      Participant
      Topics: 4
      Replies: 6
      Points: 44
      Rank: Member

      Thanks for the quick responses.

      I like cheese – I thought I was performing an operation on the value of the variable

      Olaf – Assigning it worked.

      I am actually using the trim in a ForEach loop, is it possible to do something like this

      Get-ADUser -Filter {userPrincipalName -eq $user.trim() }

      or would I need to assign it first?

      -Chris

    • #176659
      Participant
      Topics: 4
      Replies: 6
      Points: 44
      Rank: Member

      I Like Cheese & Olaf

      Had a chance to think about your responses more and look at it on my computer.  Thank you both for helping clear up my understanding of Trim/TrimEnd

      -Chris

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