Monthly Archive

Categories

Find duplicate characters in a string

Staying with our current theme of manipulating strings this is how you find duplicate characters in a string.

function get-duplicatechar {
[CmdletBinding()]
param (
[string]$teststring
)

$teststring.ToCharArray() |
Group-Object -NoElement |
where Count -gt 1 |
Sort-Object -Property Count -Descending

}

 

Convert the string to a char array, group on the characters and use Where-Object to filter the characters that occur more than once – the duplicates. Sort the output for easier viewing.

Comments are closed.