How to remove extra spaces

Today I needed to clean up some input which contained superfluous spaces. So I wrote a small oneliner powershell, to easy my work.

PS C:> $inputstring = 'The quick brown   fox jumps  over the       lazy dog'
PS C:> $inputstring -Replace "( ) *","$1"
The quick brown fox jumps over the lazy dog

What the oneliner does is quite easy, It looks for space which is followed by a free amount of spaces and then replaces all those with just the first space. Instead of the first space you could replace
$1 with just about anything, or just a space..

Leave a Reply