Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Duplicates from a String...

Status
Not open for further replies.

Inquisitor99

Programmer
Aug 25, 2005
24
US
Does anyone know a quick and efficient method to strip duplicates from a string?

For example:

My string = "AB, CD, AB, EF"

My string without duplicates = "AB, CD, EF"

Thanks
 
This assumes your example is representative
Code:
Public Function getNoDupString(strDupString As String)
  Dim strDupArray() As String
  Dim intCounterI As Integer
  Dim intCounterJ As Integer
  Dim intFirstPos As Integer
  strDupArray = split(strDupString, ",")
  For intCounterI = 0 To UBound(strDupArray)
     For intCounterJ = intCounterI + 1 To UBound(strDupArray)
       If Trim(strDupArray(intCounterJ)) = Trim(strDupArray(intCounterI)) Then
         strDupArray(intCounterJ) = ""
       End If
     Next intCounterJ
  Next intCounterI
  For intCounterI = 0 To UBound(strDupArray)
    If Not strDupArray(intCounterI) = "" Then
      getNoDupString = getNoDupString & strDupArray(intCounterI) & ","
    End If
  Next intCounterI
  If Right(getNoDupString, 1) = "," Then
    getNoDupString = Left(getNoDupString, Len(getNoDupString) - 1)
  End If
End Function

?getNoDupString("AB,CC,CB, CC ,EE ,FF,CC,AB,GG,")
AB,CC,CB,EE,FF,GG
 
Thanks MajP. I initially posted this in the 'Forms' forum and then decided it should have been posted here. I replied to the posts in the 'Forms' forum, so you can check there for my reply. Thanks for your suggestion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top