This little function lets you remove characters from a string. The twist is that it takes a character array, and will remove all characters in it from the input string.
This can come in handy if you need to for instance remove all special characters from a string. Please note that I have added a selection of special characters as default value for the Remove parameter.
If you take it for a spin, I’d love to hear your comments about it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-Characters { | |
<# | |
.SYNOPSIS | |
Remove characters from a string. | |
.DESCRIPTION | |
This function will take an array of characters, and will remove | |
those characters from the input string. | |
.EXAMPLE | |
$myString | Remove-Characters | |
This will remove all 'default' characters from myString. | |
.EXAMPLE | |
Remove-Characters -InputString $myString -Remove ([char[]](0..47 + 58..64 + 91..96 + 123..255 | ForEach-Object {[char]$_})) | |
This will remove all characters in the ASCII table except letters and numbers. | |
.EXAMPLE | |
Remove-Characters -InputString $myString -Remove ([char[]](0..47 + 58..64 + 91..96 + 123..[int][char]::MaxValue | ForEach-Object {[char]$_})) | |
This will remove all special characters with the exception of letters and numbers. NOTE This one is a bit slower than the other two examples. | |
.INPUTS | |
System.String | |
.OUTPUTS | |
System.String | |
.LINK | |
https://communary.wordpress.com/ | |
.NOTES | |
This code was translated and adapted from this StackOverflow answer: http://stackoverflow.com/a/1120407/3940558 | |
Author: Øyvind Kallstad | |
Date: 11.02.2016 | |
Version: 1.0 | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string] $InputString, | |
# Characters to remove. Default value is '!"#¤%&€/()=?`+[]{}@£$\¨^~*-_.:,;<> ' | |
[Parameter(Position = 1)] | |
[ValidateNotNullOrEmpty()] | |
[char[]] $Remove = '!"#¤%&€/()=?`+[]{}@£$\¨^~*-_.:,;<> ' | |
) | |
[char[]]$buffer = New-Object –TypeName System.Char[] –ArgumentList $InputString.Length | |
$index = 0 | |
foreach ($character in $InputString.ToCharArray()) { | |
if (-not ($Remove -contains $character)) { | |
$buffer[$index] = $character | |
$index++ | |
} | |
} | |
Write-Output (New-Object –TypeName System.String –ArgumentList ($buffer,0,$index)) | |
} |
Hi Øyvind,
Maybe I’m missing the point, but couldn’t you rather use something like the below instead?:
[char[]]$Remove = ‘!”#¤%&€/()=?`+[]{}@£$\¨^~*-_.:,;’
$inputstring = ‘test!”#¤%&€/()##=?`+[]test{}@£$\¨^~*+*-_.:,;test’
$InputString -replace “$([regex]::Escape($Remove).Replace(‘\ ‘,’|’))”, ”
LikeLike
Hi Dirk. Thanks for your comment. You most certainly could. That’s the beauty of it isn’t it? Many ways to Rome and all that 🙂 The reason I didn’t use regex was just performance really. I have read that using regex will take quite a bit longer, but you will hardly notice it unless you try it on a really large string.
LikeLike
Hi Øyvind, very true :-).
In relation to this topic, there is also a very interesting post from Tobias Weltner http://powershell.com/cs/blogs/tobias/archive/2011/04/28/multiple-text-replacement-challenge.aspx not sure if you came across it already.
LikeLiked by 1 person