Removing Unwanted Characters

21 06 2010

Suppose you have a variable and you want to remove all unwanted characters from it – for example commas.

In PowerShell, this is as easy as:

PS C:\> 'a,,b,c,d,' -replace ','
abcd

To remove multiple unwanted characters, simply chain more -replace operators on the end (note that some characters must be escaped, as we are using a regex the escape character is a backslash):

PS C:\> '.a,a\b,b\c,b\d\,d.\' -replace ',' -replace '\\' -replace '\.'
aabbcbdd

Advertisement

Actions

Information

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s




Follow

Get every new post delivered to your Inbox.