In PowerShell, removing a space or multiple white lines is an easy task.
Below are two methods on how to remove trailing, leading or just simply remove any space found on a string or variable.
Here's the code and example, with its image output:
#Method 1
$string_with_spaces= " I am a string with with leading and trailing space "
$count_the_characters = $string_with_spaces.Length
write-output ("Number of characters before trim: " + $count_the_characters)
Check out this link to remove line feed or enter key:
https://quickbytesstuff.blogspot.com/2017/06/powershell-remove-line-feed.html
Below are two methods on how to remove trailing, leading or just simply remove any space found on a string or variable.
Here's the code and example, with its image output:
#Method 1
$string_with_spaces= " I am a string with with leading and trailing space "
$count_the_characters = $string_with_spaces.Length
write-output ("Number of characters before trim: " + $count_the_characters)
The PowerShell script:
#Method 1
$string_with_spaces= " I am a string with with leading and trailing space "
$var_string_no_space = $string_with_spaces.trim()
Write-Output $var_string_no_space
$count_the_characters = $var_string_no_space.Length
write-output ("Number of characters after trim: " + $count_the_characters)
Here's the output and the number of characters after trimming the space.
Before removing or trimming the string there were 59 characters, from above image after removing the space; the number of characters becomes 50. So, there were 9 spaces that were removed.
Counting the characters or getting the length is one way to check whether trimming works since a space is a white space and cannot be recognize from the output.
Here's another method to remove the space character that it can find on the string.
#Method 2
$string_with_spaces= " I am a string with with leading and trailing space and also spaces in between. "
write-output ("")
write-output ("The Original String: " + $string_with_spaces)
write-output ("")
$var_string_no_space = $string_with_spaces -replace " "
write-output ("String output no more social distancing between words: " + $var_string_no_space )
Sample Output:
Cheers. Till next time. Hope it helps. :)
Check out this link to remove line feed or enter key:
https://quickbytesstuff.blogspot.com/2017/06/powershell-remove-line-feed.html
================================
Free Android Apps:
Click links below to find out more:
Excel Keyboard guide:
Heaven's Dew Fall Prayer app for Android :
Catholic Rosary Guide for Android:
Comments
Post a Comment