Results 1 to 4 of 4

Thread: [RESOLVED] How to display duplicate string in text box?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Resolved [RESOLVED] How to display duplicate string in text box?

    Dear all expert programmers,
    Please tell me how to hightlight duplicate string in text box. Please see example below

    Sample1 :
    Cat Bat Mat Eat Cow Start Begin Cat
    Duplicate string is Cat


    Sample2 :
    Cat Bat Cow Mat Eat Cow Start Begin Cat
    Duplicate string is Cat, Cow

    Thank you for all answers.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to display duplicate string in text box?

    Use Split function to split the string into an array, then loop through it to find duplicates.

  3. #3
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: How to display duplicate string in text box?

    Here's a HORRIBLE way to do it:
    Code:
    Private Function ListDupes(str As String) As String
        Dim sS() As String, x As Long, dupe As String, wow As String
        sS = Split(str & " ")
        For x = 0 To UBound(sS) - 1
            If InStrB(1, dupe, ", " & sS(x)) > 0 Then
                wow = wow & ", " & sS(x)
            End If
            dupe = dupe & ", " & sS(x)
        Next x
        If LenB(wow) > 0 Then
            wow = Mid$(wow, 3)
        End If
        ListDupes = wow
    End function

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How to display duplicate string in text box?

    Adding a bit to what baja_yu suggested: Split, sort and loop through. Sorting makes it easier to find the duplicates, because the same ones are in a group, so you don't need to loop through all other items for each item to find the duplicates.

    Here is something that isn't often suggested: Filter.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim strArray() As String, lngUB As Long, strRemove As String
        strArray = Split("Cat Bat Cow Mat Eat Cow Start Begin Cat")
        Do While UBound(strArray) > 0
            lngUB = UBound(strArray)
            strRemove = strArray(0)
            strArray = Filter(strArray, strRemove, False)
            If UBound(strArray) + 1 < lngUB Then Debug.Print strRemove
        Loop
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width