Solved

How to check string for duplication of information

Posted on 2011-03-18
9
235 Views
Last Modified: 2012-08-13
Hi experts

I have a string of text called myString. It contains a few lines of an address.
I want to check this string to see if there is a duplication of some of the lines of data. The data that doubles up would appear something like this (Upper case then lower case):

ABC PTY LIMITED
ABC Pty Limited
Address lIne 1
Address line 2

Assistance would be much appreciated.
0
Comment
Question by:Fi69
  • 5
  • 3
9 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35169505
Something like this?
Dim line1 As String ' Load with first line
Dim line2 As String ' Load with second line

If UCase(Trim(line1)) = UCase(Trim(line2)) Then
    ' Duplicate exists
End If

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 35169822
This function should detect duplicate lines in your addresses
Option Explicit

Public Function HasDupLines(parmAddress As String) As Boolean
  Dim strLines() As String
  Dim lngComp As Long
  Dim lngWith As Long
  strLines = Split(parmAddress, vbCrLf)
  HasDupLines = False
  For lngComp = 0 To UBound(strLines) - 1
    For lngWith = lngComp To UBound(strLines)
      If StrComp(strLines(lngComp), strLines(lngWith), vbTextCompare) = 0 Then
        HasDupLines = True
        Exit Function
      End If
    
    Next
  
  Next

End Function

Open in new window

0
 

Author Comment

by:Fi69
ID: 35169860


Thank you aikimark that function is returning a true value for the duplicate lines.
kaufmed, I realised that may actually not be line 1 and line 2, in may be line 2 and 3 or 3 and 4 that contain the duplicate.

aikimar -- how do I then tell it to delete the first appearance of the duplicate line?

0
 
LVL 45

Expert Comment

by:aikimark
ID: 35169992
The following function does that.

Notes:
* There is no check for empty strings in either function, so it might be possible that the code might detect two empty string address lines as identical
* Neither routine checks for multiple matches.  You might need to place the invocation of these functions into a loop of your address lines might contain 3 identical lines.
Option Explicit

Public Function DelFirstDupLine(parmAddress As String) As Boolean
  Dim strLines() As String
  Dim lngComp As Long
  Dim lngWith As Long
  strLines = Split(parmAddress, vbCrLf)
  HasDupLines = False
  For lngComp = 0 To UBound(strLines) - 1
    For lngWith = lngComp To UBound(strLines)
      If StrComp(strLines(lngComp), strLines(lngWith), vbTextCompare) = 0 Then
        strLines(lngComp)=vbNullString
        HasDupLines = True
        Exit Function
      End If
    
    Next
  
  Next

End Function

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Fi69
ID: 35170107
Hi, sorry now I need to understand how to get the 'culled' string back into where I need it.

So in one module I have a procedure that has the value of the original string called strRecipientAddress
Then I need to look at that address and access if there are duplicate lines and delete the duplicate lines if they exist (there would be only one repeat occurrence).

My head is telling me the way to do that using your function is
strRecipientAddress = delfirstdupline(strRecipientAddress) but it returns a true/false value.

Are you able to guide me some more. I've upped the point value on this answer to 500.

0
 
LVL 45

Expert Comment

by:aikimark
ID: 35171688
sorry about that.  I forgot to update the parameter.  The function (as now written) will still return a boolean, but will now change the string parameter.

You are certainly welcome to change it to return a string of the changed address.
Option Explicit

Public Function DelFirstDupLine(parmAddress As String) As Boolean
  Dim strLines() As String
  Dim lngComp As Long
  Dim lngWith As Long
  strLines = Split(parmAddress, vbCrLf)
  HasDupLines = False
  For lngComp = 0 To UBound(strLines) - 1
    For lngWith = lngComp To UBound(strLines)
      If StrComp(strLines(lngComp), strLines(lngWith), vbTextCompare) = 0 Then
        strLines(lngComp)=vbNullString
        HasDupLines = True
        parmAddress = Join(strLines, vbCrLf)
        Exit Function
      End If
    
    Next
  
  Next

End Function

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 35311269
@Fi69

Are you sure that http:#35169505 solved your problem?
0
 

Author Comment

by:Fi69
ID: 35313257
Hi Aikimark

Sorry, I didn't award you points. Being a somewhat novice I couldn't understand how to get your function to get rid of the duplicated lines of the address. I wasn't sure what to do with the Boolean. I could have pursued it with you, but then I discovered that the 2 lines of text that could duplicate were coming in from two different fields and I was able to use kaufmed's reply to compare line 2 and line1 of those fields. Thank you for trying to help me.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 35313603
If you need help in your future questions, please ask the expert whose code you are having a problem with.

=====
For future readers, the following routine will remove all duplicates and return the 'cleaned' address.  An optional parmKeepFirst parameter allows you to specify whether to keep the first or second instance(s) of duplicate lines.
Option Explicit

Public Function DelDupLines(parmAddress As String, Optional parmKeepFirst As Boolean = True) As String
  Dim strLines() As String
  Dim lngComp As Long
  Dim lngWith As Long
  strLines = Split(parmAddress, vbCrLf)

  For lngComp = 0 To UBound(strLines) - 1
    For lngWith = lngComp + 1 To UBound(strLines)
      If StrComp(strLines(lngComp), strLines(lngWith), vbTextCompare) = 0 Then
        If parmKeepFirst Then
          strLines(lngWith) = vbNullString
        Else
          strLines(lngComp) = vbNullString
        End If
        DelDupLines = Join(strLines, vbCrLf)
      End If
    
    Next
  
  Next

End Function

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now