Home » PowerShell » Split Special Characters

Split Special Characters

Recent Tweets

Enter your email address to follow this blog and receive notifications of new posts by email.

Advertisements

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($_) }
Hello

 this is a test

Use this line to view all available character groups:

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

[Char]::isPunctuation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: