Results 1 to 8 of 8

Thread: [RESOLVED] How to get duplicate character?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Resolved [RESOLVED] How to get duplicate character?

    Dear all expert programmer,
    I want to know how to get duplicate character in data. If I have data below.

    tmpStr = "abcd,bcde,cdef,dehmo"

    Duplicate character is "d" but I can't code it. Please tell me how to get duplicate character.


    Thank you for all post.

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to get duplicate character?

    tmpStr = "abcd,bcde,cdef,dehmo"

    "d" is duplicate because you can see it in all group (seperate group by ,), If you can not see it in all group it is not duplicate.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to get duplicate character?

    Code:
     Const TEST_DATA = "abcd,bcde,cdef,dehmo"
     Dim strParts() As String
     Dim lngNdxOfString1 As Long
     Dim lngNdxOfRest As Long
     Dim bInAll As Boolean
     Dim strTemp As String
      
     strParts = Split(TEST_DATA, ",")
     
     For lngNdxOfString1 = 1 To Len(strParts(0))
        bInAll = True
        For lngNdxOfRest = 1 To UBound(strParts)
            strTemp = Replace(strParts(lngNdxOfRest), Mid$(strParts(0), lngNdxOfString1, 1), "")
            If Len(strTemp) = Len(strParts(lngNdxOfRest)) Then
                bInAll = False
                Exit For
            End If
        Next
        If bInAll Then
            MsgBox Mid$(strParts(0), lngNdxOfString1, 1) & " is duplicate"
        End If
     Next

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to get duplicate character?

    Please test modified code and help me again.

    vb Code:
    1. Private Function GetDuplicatechar(ByVal strSource As String) As String
    2.     Dim strParts() As String
    3.     Dim lngNdxOfString1 As Long
    4.     Dim lngNdxOfRest As Long
    5.     Dim bInAll As Boolean
    6.     Dim strTemp As String
    7.     Dim strResultX As String
    8.     strParts = Split(strSource, ",")
    9.     strResultX = ""
    10.     For lngNdxOfString1 = 1 To Len(strParts(0))
    11.         bInAll = True
    12.         For lngNdxOfRest = 1 To UBound(strParts)
    13.             strTemp = Replace(strParts(lngNdxOfRest), Mid$(strParts(0), lngNdxOfString1, 1), "")
    14.             If Len(strTemp) = Len(strParts(lngNdxOfRest)) Then
    15.                 bInAll = False
    16.                 Exit For
    17.             End If
    18.         Next
    19.         If bInAll Then
    20.             strResultX = Mid$(strParts(0), lngNdxOfString1, 1)
    21.         End If
    22.     Next
    23.     GetDuplicatechar = strResultX
    24. End Function
    25.  
    26. Private Sub Command1_Click()
    27.     MsgBox GetDuplicatechar("4582,4582") 'It will show 4582
    28. End Sub
    Last edited by standardusr; Dec 10th, 2007 at 12:35 AM.

  6. #6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to get duplicate character?

    Thank you.

    If the code is
    vb Code:
    1. Private Sub Command1_Click()
    2.     MsgBox GetDuplicatechar("4582,")
    3. End Sub

    Why it is empty string?

  8. #8

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