Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Dyndrilliac on November 01, 2004, 12:23 PM

Title: [VB] Duplicate Characters in a String
Post by: 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.
Title: Re: [VB] Duplicate Characters in a String
Post by: Adron on November 01, 2004, 12:41 PM
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?
Title: Re: [VB] Duplicate Characters in a String
Post by: Dyndrilliac on November 01, 2004, 01:06 PM
Thanks :D