Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove space when combining variables in PowerShell

Tags:

powershell

Would like to know how to remove space when combining string variables in below code. Cause when I count the length of $myname, it will also include the space length

$myname = $firstname + " " + $lastname
like image 703
Johan Avatar asked May 08 '26 16:05

Johan


1 Answers

Use replace and regex to replace spaces with nothing before you do the length function. Last $myname is just to prove we didn't actually remove spaces in the variable while getting the length.

$LastName = "Smith"
$FirstName = "John"
$myname = $Firstname + " " + $Lastname
$myname
($myname -replace "\s","").length
$myname

Output

John Smith
9
John Smith
like image 86
Diabetic4Life Avatar answered May 11 '26 16:05

Diabetic4Life



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!