locked
PowerShell Trim,TrimStart() method not working on a String RRS feed

  • Question

  • Hi ,

    The Trim, TrimStart methods is not able to eliminate space in a string .Please help out with the suggestions .

    Thanks in Advance !!

    Thursday, November 15, 2018 12:56 PM

Answers

  • Hi  

    The replace function suggested didn't work :(.


    Works fine if you use it correctly:

    PS D:\scripts> $hostnamenew =$hostname -replace '\s'
    PS D:\scripts> $hostnamenew
    soemspvestotest

    or

    PS D:\scripts>  $hostname = '  soem spves to test  '
    PS D:\scripts> $hostname.Replace(' ','')
    soemspvestotest


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, November 15, 2018 2:14 PM
    • Marked as answer by Richard MuellerMVP Thursday, November 22, 2018 1:47 PM
    Thursday, November 15, 2018 2:13 PM

All replies

  • Those remove spaces from the beginning or both ends of a string.  To remove all spaces use "-replace".


    \_(ツ)_/

    Thursday, November 15, 2018 1:01 PM
  • $hostnamenew =$hostname -replace(" ","") this line the replace works fine .

    $sp="ABC"+"|"+"A"+"|"+$hostnamenew -replace(" ","")+"|"+"RSA"+"|"+$($user[$i])+"|"+" "+"|"+" "+"|"+"Enabled"+"|"+"date"+"|"+" "+"|"+$($correctroles[$i])|Out-File $mefpathvalue"\"$customervalue"_"$date$month$year"_"$hostname.mef3 -Append

     this place the replace function doesn't work. 

    Thursday, November 15, 2018 1:18 PM
  • Please format your code as code. Otherwise it will have unwanted line breaks and will not work.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, November 15, 2018 1:23 PM
  • Hi  

    The replace function suggested didn't work :(.


    Works fine if you use it correctly:

    PS D:\scripts> $hostnamenew =$hostname -replace '\s'
    PS D:\scripts> $hostnamenew
    soemspvestotest

    or

    PS D:\scripts>  $hostname = '  soem spves to test  '
    PS D:\scripts> $hostname.Replace(' ','')
    soemspvestotest


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, November 15, 2018 2:14 PM
    • Marked as answer by Richard MuellerMVP Thursday, November 22, 2018 1:47 PM
    Thursday, November 15, 2018 2:13 PM
  • Please format your code as code. Otherwise it will have unwanted line breaks and will not work.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    There is a bad character on this line:

     [Switch]$Help,`

    note the extra character.  This messes up the code colorizer.


    \_(ツ)_/

    Thursday, November 15, 2018 2:15 PM