1

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?

Yuck
  • 49,664
  • 13
  • 105
  • 135
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • 2
    I kind of thought that `-` was just a hyphen. What would be a **hard** hyphen? – Yuck Feb 06 '12 at 19:10
  • 1
    @Yuck http://en.wikipedia.org/wiki/Soft_hyphen - and what Luke101 is doing is actually a hard hyphen, not a soft one. A hard hyphen stays with the text no matter what, as in mother-in-law. – Lunivore Feb 06 '12 at 19:13
  • @L.B You can write C# cmdlets to do stuff in Powershell. – Lunivore Feb 06 '12 at 19:14
  • 1
    A soft hyphen would be ‘­’. You probably won’t see it in a browser, but if you copy-and-paste my previous sentence to Notepad, it will automagically appear. If you’re on Windows, you might be able to create it by typing Alt+0173. – Douglas Feb 06 '12 at 19:15
  • Here is a good link to look at in regards to Scripting with PowerShell.. http://technet.microsoft.com/en-us/scriptcenter/dd742419 – MethodMan Feb 06 '12 at 19:16

4 Answers4

6

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())
Community
  • 1
  • 1
Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
6

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

2

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.

Lunivore
  • 17,277
  • 4
  • 47
  • 92
0

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.

Jeffery Hicks
  • 947
  • 5
  • 8