Re: Find words within range of other word in string?????????

Tech-Archive recommends: Fix windows errors by optimizing your registry



Here is a word separator routine that is not based on regular expressions

Option Explicit

Private Sub Form_Load()
Const WordSeparators As String = " .,:;'""()[]{}`~@#$%^&/*+-=|\<>?"
Dim MyText As String
Dim i As Long
Dim Words(100) As String ' up to 100 words, 1 based array
Dim WordsCount As Long
Dim LastWord As String
Dim ch As String

MyText = "abc. def, ghi,jklm; nopqr"
LastWord = ""
For i = 1 To Len(MyText)
ch = Mid(MyText, i, 1)
If InStr(WordSeparators, ch) <> 0 Then
' The char is in WordSeparators
If Len(LastWord) > 0 Then
' End of word, save it
WordsCount = WordsCount + 1
Words(WordsCount) = LastWord
LastWord = "" ' Reset for the next word
End If
Else
' The char is a valid word char, add it to the word
LastWord = LastWord & ch
End If
Next
' The last word in the text may not have ended with a separator, so
' add it when needed
If Len(LastWord) > 0 Then
' End of word, save it
WordsCount = WordsCount + 1
Words(WordsCount) = LastWord
LastWord = "" ' Reset for the next word
End If

' Print out the list of words
For i = 1 To WordsCount
Debug.Print i, "'"; Words(i); "'"
Next

End Sub

The sample shows:

1 'abc'
2 'def'
3 'ghi'
4 'jklm'
5 'nopqr'


.



Relevant Pages

  • Re: Anagram Algorithm - How can I improve it?
    ... Given a string, invokes a loop, calling ScrambleWord. ... Function CheckWordExists(ByVal wordToCheck As String) As Long ... Dim firstWord As String, lastWord As String ...
    (microsoft.public.dotnet.languages.vb)
  • Anagram Algorithm - How can I improve it?
    ... Given a string, invokes a loop, calling ScrambleWord. ... Function CheckWordExists(ByVal wordToCheck As String) As Long ... Dim firstWord As String, lastWord As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Anagram Algorithm - How can I improve it?
    ... Then I would search the words while using inside the word string the Find because that is the fasted for that. ... Function CheckWordExists(ByVal wordToCheck As String) As Long ... Dim firstWord As String, lastWord As String ...
    (microsoft.public.dotnet.languages.vb)