Welcome › Forums › General PowerShell Q&A › Replace operator with one parameter
This topic contains 3 replies, has 3 voices, and was last updated by
I found this bit of code which removes the last character of a string:
$string -replace '.$'
However the about_comparison_operators clearly states that replace takes two parameters. There is obviously an undocumented feature where the second parameter defaults to ""?
If so, why wasnt it included in the official MS doco? Or is it documented somewhere else?
Maybe someone -replace'd it out of existence.
The about_ page is a little ambiguous, in my opinion.
The substitute argument is optional, and if left blank, will just remove the matched string.
Yep, it's the same as having a 2nd arg as empty string or null.
PS C:\users\js> 'sosososo' -replace 's',$null
A lot of the operators work on arrays as well.
PS C:\users\js> 'sosososo','sasasasa','sesesese' -replace 's'
You must be logged in to reply to this topic.