How to cut a string into individual words

4 posts / 0 new
Last post
CB's picture
CB
Offline
Joined: 10/19/2012 - 04:54
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

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Chris, thanks for sharing this script.  I am sure I will find use for it at some point.

Brian

Lois's picture
Lois
Offline
Joined: 02/14/2013 - 09:00

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

CB's picture
CB
Offline
Joined: 10/19/2012 - 04:54

Hi Lois, sure you could use the Strings to create an new field. Have a look at http://ideascripting.com/forum/get-error-messages-or-notes-persistent. There you find a description to Create Fields via IdeaScript.