Valhalla Legends Forums Archive | Visual Basic Programming | [VB] Duplicate Characters in a String

AuthorMessageTime
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.
November 1, 2004, 6:23 PM
Adron
[quote author=Dyndrilliac link=topic=9391.msg87032#msg87032 date=1099333405]
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]

[code]
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
[/code]

Did I ever mention I wish the collection object had a way of checking for existence of a key without having to catch errors?
November 1, 2004, 6:41 PM
Dyndrilliac
Thanks :D
November 1, 2004, 7:06 PM

Search