[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.
Re: How to get duplicate character?
Isn't "e" (and others) also duplicate?
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.
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
Re: How to get duplicate character?
Please test modified code and help me again.
vb Code:
Private Function GetDuplicatechar(ByVal strSource As String) As String
Dim strParts() As String
Dim lngNdxOfString1 As Long
Dim lngNdxOfRest As Long
Dim bInAll As Boolean
Dim strTemp As String
Dim strResultX As String
strParts = Split(strSource, ",")
strResultX = ""
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
strResultX = Mid$(strParts(0), lngNdxOfString1, 1)
End If
Next
GetDuplicatechar = strResultX
End Function
Private Sub Command1_Click()
MsgBox GetDuplicatechar("4582,4582") 'It will show 4582
End Sub
Re: How to get duplicate character?
Change the highlighted part.
Code:
If bInAll Then
strResultX = strResultX & Mid$(strParts(0), lngNdxOfString1, 1)
End If
Re: How to get duplicate character?
Thank you.
If the code is
vb Code:
Private Sub Command1_Click()
MsgBox GetDuplicatechar("4582,")
End Sub
Why it is empty string?
Re: How to get duplicate character?
Because there is no duplicate.