Saturday, August 27, 2011

PowerShell: Remove Duplicate Characters In a String

I couldn't find a function to remove duplicates in a string, so I made this one.
Given AABBBC, it will will return ABC.





Function RemoveDuplicates($strString) {
  For ($i = 1; $i -lt $strString.length; $i++) { 
    $strString = $strString.Substring(0, $i) + `
    $strString.Substring($i).`
    replace($strString.`
    Substring($i-1, 1), "")
  }
  $strString
}

No comments: