Skip to main content

How to cut a string into individual words

 
Hi,
 
maybe someone needs something like this.
 
Here is a short Example:
 


Sub Main

Dim sOriginalText As String
Dim sOriginalText_C As String
Dim sWord As String
Dim iNumBlank As Integer

sOriginalText = ""
sOriginalText_C = ""
sWord = ""
iNumBlank = 0

sOriginalText = "42 : , ; .. How many roads must a man walk down Before you Call him a man How many seas must a white dove sail Before she sleeps In the sand"

'01. maybe you have to eliminate some text character like , : ;
sOriginalText_C = iReplace(iReplace(iReplace(iReplace(sOriginalText, ",", ""), ":", ""), ";", ""), ".", "")

' 02. lower the original text and eliminate the blanks
sOriginalText_C = iLower( iAllTrim( sOriginalText_C) )

'03. if the string is empty do nothing more
If iIsBlank(sOriginalText_C) = 1 Then
Exit Sub
End If

'04. cut the string into individual words to check them
Do Until iIsBlank(sOriginalText_C) = 1

iNumBlank = iFindOneOf (sOriginalText_C, " ")

If iNumBlank > 0 Then
sWord = iMid(sOriginalText_C, 1, iNumBlank-1)

If sWord = "sleeps" Then
MsgBox "sleeps found.... exit",0,"Exit loop"
Exit Do
End If
Else
sWord = sOriginalText_C

If sWord = "sleeps" Then
MsgBox "sleeps found.... exit",0,"Exit loop"
Exit Do
End If
End If

If iNumBlank > 0 Then
sOriginalText_C = iAllTrim(iDelete(sOriginalText_C , 1, iNumBlank))
Else
sOriginalText_C = iAllTrim(iReplace(sOriginalText_C, sWord, ""))
End If

MsgBox "Original String: " & Chr(13) & sOriginalText & Chr(13) & Chr(13) & _
"Word: " & Chr(13) & sWord & Chr(13) & Chr(13) & _
"New String: " & Chr(13) & sOriginalText_C _
,0,"Check - how it works"
Loop

sOriginalText = ""
sOriginalText_C = ""
sWord = ""
iNumBlank = 0

End Sub

 
 
Cheers,
Chris

Lois Tue, 06/05/2018 - 10:17

Chris thanks... It would prove helpful. Just wondering, can I use it to create a new field instead of displaying as a message?
Lois