Re: Find words within range of other word in string?????????
- From: "expvb" <nobody@xxxxxxx>
- Date: Fri, 4 Aug 2006 13:09:32 -0400
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'
.
- Follow-Ups:
- Re: Find words within range of other word in string?????????
- From: Larry Serflaten
- Re: Find words within range of other word in string?????????
- Prev by Date: ole questions
- Next by Date: Re: Slot Machine Code
- Previous by thread: ole questions
- Next by thread: Re: Find words within range of other word in string?????????
- Index(es):
Relevant Pages
|