Split Special Characters

by Mar 30, 2011

PowerShell’s new –split operator can split text into parts. Usually, you will submit a delimiter character to tell –split where to split:

PS> "1,2,3,4" -split ","
1
2
3
4

However, you can also submit a script block and calculate where to split. If you take advantage of the many specific character tests available in the [Char] type, you can then split based on punctuation or other character groups:

"Hello, this is a test" -split { [Char]::isPunctuation($_) }

Use this line to view all available character groups:

[char] | Get-Member -Static -MemberType Method -Name is*
[Char]::isPunctuation

 

Twitter This Tip!
ReTweet this Tip!