34

I've tried a few things already but they don't seem to work for some reason.

Basically what I'm attempting to do is have a user input a value using the "Read-host" cmdlet, then strip it of any spaces.

I tried:

$answer = read-host
$answer.replace(' ' , '""')

And:

$answer = read-host
$answer -replace (' ')

I'm probably missing something really obvious, but if anyone could help me out or show me an easier way to achieve this I would appreciate it.

I was going to pipeline the variable to a command and strip it in that fashion, but none of the examples I've seen work, although they look much easier.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
cloudnyn3
  • 769
  • 4
  • 10
  • 15

5 Answers5

71

The Replace operator means Replace something with something else; do not be confused with removal functionality.

Also you should send the result processed by the operator to a variable or to another operator. Neither .Replace(), nor -replace modifies the original variable.

To remove all spaces, use 'Replace any space symbol with empty string'

$string = $string -replace '\s',''

To remove all spaces at the beginning and end of the line, and replace all double-and-more-spaces or tab symbols to spacebar symbol, use

$string = $string -replace '(^\s+|\s+$)','' -replace '\s+',' '

or the more native System.String method

$string = $string.Trim()

Regexp is preferred, because ' ' means only 'spacebar' symbol, and '\s' means 'spacebar, tab and other space symbols'. Note that $string.Replace() does 'Normal' replace, and $string -replace does RegEx replace, which is more heavy but more functional.

Note that RegEx have some special symbols like dot (.), braces ([]()), slashes (\), hats (^), mathematical signs (+-) or dollar signs ($) that need do be escaped. ( 'my.space.com' -replace '\.','-' => 'my-space-com'. A dollar sign with a number (ex $1) must be used on a right part with care

'2033' -replace '(\d+)',$( 'Data: $1')
Data: 2033

UPDATE: You can also use $str = $str.Trim(), along with TrimEnd() and TrimStart(). Read more at System.String MSDN page.

Logan
  • 1,575
  • 1
  • 17
  • 26
filimonic
  • 3,988
  • 2
  • 19
  • 26
  • 1
    The answer can be confusing for a student/learner because you refer to other replaces besides whitespace. – Timo Oct 26 '20 at 17:04
9

You're close. You can strip the whitespace by using the replace method like this:

$answer.replace(' ','')

There needs to be no space or characters between the second set of quotes in the replace method (replacing the whitespace with nothing).

Kohlbrr
  • 3,861
  • 1
  • 21
  • 24
6

You also have the Trim, TrimEnd and TrimStart methods of the System.String class. The trim method will strip whitespace (with a couple of Unicode quirks) from the leading and trailing portion of the string while allowing you to optionally specify the characters to remove.

#Note there are spaces at the beginning and end
Write-Host " ! This is a test string !%^ "
 ! This is a test string !%^
#Strips standard whitespace
Write-Host " ! This is a test string !%^ ".Trim()
! This is a test string !%^
#Strips the characters I specified
Write-Host " ! This is a test string !%^ ".Trim('!',' ')
This is a test string !%^
#Now removing ^ as well
Write-Host " ! This is a test string !%^ ".Trim('!',' ','^')
This is a test string !%
Write-Host " ! This is a test string !%^ ".Trim('!',' ','^','%')
This is a test string
#Powershell even casts strings to character arrays for you
Write-Host " ! This is a test string !%^ ".Trim('! ^%')
This is a test string

TrimStart and TrimEnd work the same way just only trimming the start or end of the string.

StephenP
  • 3,895
  • 18
  • 18
4

You can use:

$answer.replace(' ' , '')

or

$answer -replace " ", ""

if you want to remove all whitespace you can use:

$answer -replace "\s", ""
Lee
  • 142,018
  • 20
  • 234
  • 287
  • What is the diff between \s and ' ' (first and third example)? – Timo Oct 26 '20 at 17:09
  • 1
    @Timo - `' '` is a string containing a single space so only that character will be matched. "\s" is a regex matching any whitespace so the third example removes all whitespace and not just spaces. – Lee Oct 27 '20 at 00:30
  • Thanks Lee so \s is much saver to remove all spaces than ' ' because you can easily type 2 spaces while your intent was just one. – Timo Oct 27 '20 at 07:06
1

If the string is

$STR = 'HELLO WORLD'

and you want to remove the empty space between 'HELLO' and 'WORLD'

$STR.replace(' ','')

replace takes the string and replaces white space with empty string (of length 0), in other words the white space is just deleted.

David
  • 5,882
  • 3
  • 33
  • 44
Abs
  • 11
  • 1