Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

UniqueWords - Extract all individual words in a string
[ All Languages » VB »  String]

Total Hit ( 1694)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' build a list of all the individual words in a string
'
' returns a collection that contains all the unique words.
' The key for each item is the word itself
' so you can easily use the result collection to both
' enumerate the words and test whether a given word appears
' in the text. Words are inserted in the order they appear
' and are stored as lowercase strings.
'
' Numbers are ignored, but digit characters are preserved
' if they appear in the middle or at the end of a word.

Function UniqueWords(ByVal Text As String) As Collection
  Dim thisWord As String
  Dim i As Long
  Dim wordStart As Long
  Dim varWord As Variant
  Dim res As String

  ' prepare the result collection
  Set UniqueWords = New Collection
  
  ' ignore duplicate words
  On Error Resume Next
  
  ' extract all words from the text
  For i = 1 To Len(Text)
    Select Case Asc(Mid$(Text, i, 1))
      Case 65 To 90, 97 To 122
        ' an alpha char
        If wordStart = 0 Then wordStart = i
      Case 48 To 57
        ' include digits only if suffix of a word (as in "ABCD23")
      Case Else
        If wordStart Then
          ' extract the word
          thisWord = LCase$(Mid$(Text, wordStart, i - wordStart))
          ' add to the collection, but ignore if already there
          UniqueWords.Add thisWord, thisWord
          ' reset the flag/pointer
          wordStart = 0
        End If
    End Select
  Next
  
  ' account for the last word
  If wordStart Then
    ' extract the word
    thisWord = LCase$(Mid$(Text, wordStart, i - wordStart))
    ' add to the collection, but ignore if already there
    UniqueWords.Add thisWord, thisWord
  End If
  
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.