Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing spaces from a variable input using PowerShell 4.0

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.

like image 417
cloudnyn3 Avatar asked Jun 22 '14 21:06

cloudnyn3


People also ask

How do I Trim a variable in PowerShell?

Introduction to PowerShell Trim. PowerShell Trim() methods (Trim(), TrimStart() and TrimEnd()) are used to remove the leading and trailing white spaces and the unwanted characters from the string or the raw data like CSV file, XML file, or a Text file that can be converted to the string and returns the new string.

How do I remove spaces from a text file in PowerShell?

We can use following script to remove space at the end of each line in a file with the help of powershell script. $InputFile = 'C:\Users\user\Desktop\1. txt' write-host "removing trailing space.. of file $InputFile" $content = Get-Content $InputFile $content | Foreach {$_. TrimEnd()} | Set-Content newfile.

How do I Trim text in PowerShell?

One of the most common ways to trim strings in PowerShell is by using the trim() method. Like all of the other trimming methods in PowerShell, the trim() method is a member of the System. String . NET class.


1 Answers

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.

like image 88
filimonic Avatar answered Sep 22 '22 11:09

filimonic