• Welcome to Valhalla Legends Archive.
 

[VB] Duplicate Characters in a String

Started by Dyndrilliac, November 01, 2004, 12:23 PM

Previous topic - Next topic

Dyndrilliac

I have a string and I want to eliminate all duplicate characters - How would I do this? For example:

In "11234567" or "617415981" I would want to eliminate all the extra ones in the string.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Adron

Quote from: Dyndrilliac on November 01, 2004, 12:23 PM
I have a string and I want to eliminate all duplicate characters - How would I do this? For example:

In "11234567" or "617415981" I would want to eliminate all the extra ones in the string.


Function remdup(ByVal s As String) As String
  Dim used As New Collection
  Dim ix As Long
  On Error GoTo duplicate
  ix = 1
  Do While ix <= Len(s)
    used.Add "", Mid(s, ix, 1)
    ix = ix + 1
nextloop:
  Loop
  remdup = s
  Exit Function
duplicate:
  If Err = 457 Then s = Left(s, ix - 1) & Mid(s, ix + 1): Resume nextloop
  On Error GoTo 0
  Resume
End Function


Did I ever mention I wish the collection object had a way of checking for existence of a key without having to catch errors?

Dyndrilliac

Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.