问题
I would like to insert a soft hyphen between every letter in a word using powershell. for example here is some text:
Thisisatest => T-h-i-s-i-s-a-t-e-s-t
- is a soft hyphen. How might i do this in powershell?
回答1:
Using .NET methods a little more than canonical PowerShell code, you can write
$word = "Thisisatest"
[System.String]::Join("-", $word.ToCharArray())
and Powershell outputs "T-h-i-s-i-s-a-t-e-s-t"
EDIT: For a true soft hyphen, and using this answer on Unicode in PowerShell, I would change the second line to
[System.String]::Join([char] 0x00AD, $word.ToCharArray())
回答2:
You can use the PowerShell-friendly -join operator to do this:
"Thisisatest".ToCharArray() -join '-'
Look at the PowerShell Technet help for more information about the -join PowerShell operator.
http://technet.microsoft.com/en-us/library/dd315375.aspx
回答3:
There is a great article on splitting and joining strings in PowerShell here.
You may also find that the string.ToCharacterArray method is useful, as mentioned here.
回答4:
My Prof. PowerShell column on the topic of splitting and joining: http://mcpmag.com/articles/2011/10/18/split-and-join-operators.aspx
Personally, I think you should avoid using .NET classes and methods unless there is no "native" PowerShell cmdlet or operator.
来源:https://stackoverflow.com/questions/9165664/how-to-insert-a-soft-hyphen