How to split a string by space using PowerShell?
PowerShell provides an easy way to do it using "split" method in PowerShell.
The split method accepts any delimiter; it can be space, dash, comma or other characters as long as it has a pattern on the string.
The split method will literally split the string and the output is displayed on a separate line or separated via a line feed.
Example:
$string = "This is my test string"
$split_string_array = $string -split " "
$split_string_array
Output will be:
This
is
my
test
string
If there is a need to remove the spaces but combine the string in one line, PowerShell also provides the "join" method which basically combine or joins the string in one line.
Example:
$string = "This is my test string"
$split_string_array = $string -split " "
$join_string_array = $split_string_array -Join ""
Write-Host $join_string_array
Output:
Thisismyteststring
Using the join method, a string can be manipulated on how it would look like when the whole string or data is combined.
For example, if you need to replace "spaces" with dash. Then on the join method, put the dash character to combine or join the string.
Example:
$string = "This is my test string"
$split_string_array = $string -split " "
$join_array_dash = $array -Join "-"
Write-Host $join_array_dash
Output:
This-is-my-test-string
"Join" method in PowerShell can also be used to replace the characters that were removed by the split method.
Cheers.. hope it helps.. till next time.
==================
Free Android Apps:
Click on links below to find out more:
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
==================
Free Android Apps:
Click on links below to find out more:
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide for Android:
Pray the Rosary every day, countless blessings will be showered upon your life if you recite the Rosary faithfully.
https://play.google.com/store/apps/details?id=com.myrosaryapp
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html
Comments
Post a Comment