Shandy's Blog

Where Andrew Sutton, aka Shandy rants and rambles on as the fancy takes him

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

My name is Andrew Sutton, aka Shandy.

I am currently living and working in the UK within the garment industry as an IT specialist. This blog contains mainly IT related issues.

I was a Microsoft VB MVP for a couple of years (Apr 2004-Mar 2006) and was a vbCityLeader between April 2003 and June 2007.

If you are looking for my Sri Lanka or Morocco experiences check out Shandy's Sri Lanka Blog or Shandy's Morocco Blog. My personal (Non IT) blog is now at Shandy's Place

JunJuly 2009Aug
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Articles

Archives

Topics

Image Galleries

.NET

Blogs I Read

Others

VB.Classic

This post at vbCity piqued my interest today. I got a bit carried away with providing a solution. In retrospect I think that the use of a simple left$ statement would have solved the member's problem. Anyhow the idea of removing duplicate words from a string appealed to me and after re-working my rather bloated code I came up with the following solution:

Private Function RemoveDuplicateWords(ByVal strVvText As String _
                                     , ByVal strVvDelimiter As String _
                                     ) As String
    Dim intLvPreviousIndex As Integer: intLvPreviousIndex = 1
    Dim intLvIndex As Integer: intLvIndex = InStr(strVvText, strVvDelimiter) + 1
    Dim strLvTextSuffix As String: strLvTextSuffix = ""
    '   Need to delimiter to end of string if it is not already there
    strLvTextSuffix = IIf(Right$(strVvText, Len(strVvDelimiter)) = strVvDelimiter _
                        , "" _
                        , strVvDelimiter _
                        )
    strVvText = strVvText & strLvTextSuffix
    Do While intLvIndex > intLvPreviousIndex
        strVvText = Left$(strVvText, intLvIndex - 1) _
                  & Replace$(Mid$(strVvText, intLvIndex) _
                            , Mid$(strVvText _
                                  , intLvPreviousIndex _
                                  , intLvIndex - intLvPreviousIndex _
                                  ) _
                            , "" _
                            )
        '   Start of previous word
        intLvPreviousIndex = intLvIndex
        '   Start of next word
        intLvIndex = InStr(intLvIndex, strVvText, strVvDelimiter) + 1
    Loop
    '   Need to remove delimiter at end of string unless added at beginning of method
    strVvText = IIf(Right$(strVvText, Len(strLvTextSuffix)) = strLvTextSuffix _
                   , Left$(strVvText, Len(strVvText) - Len(strLvTextSuffix)) _
                   , strVvText _
                   )
    RemoveDuplicateWords = strVvText
End Function

I am sure there must be a better solution to the problem. Any takers?

Note that my solution only caters for a single word separator. Full stops, commas etc. could not be catered for without multiple calls to the function. Also the the first occurance is always the one left behind.

posted on Wednesday, July 20, 2005 8:19 PM