home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch20 / seditor / cntwords.scr next >
Encoding:
Text File  |  1998-07-02  |  1.2 KB  |  36 lines

  1. "Sub Main()
  2.     
  3.     Str1 = ""Welco    me to VB    Script. The s  imp       lest language ""
  4.     Str1 = Str1 & ""for scripting your      VB applications.""
  5.     Str1 = Trim(Str1)
  6.  
  7.     position = 1
  8.     ' massage string:
  9.     ' replace line feeds with spaces
  10.     Str1 = Replace(Str1, Chr(13) & Chr(10), "" "")
  11.     ' replace tabs with single spaces
  12.     Str1 = Replace(Str1, Chr(9), "" "")
  13.     Str1 = Trim(Str1)
  14.     ' Count the first word
  15.     ' Because the last word isn't delimited by
  16.     ' a space, if the string isn't blank, then it
  17.     ' contains at least one word.
  18.     ' By setting words=1, we won't have to increase the
  19.     ' number of words by 1 when we are done counting.
  20.     If Len(Str1) > 0 Then words = 1
  21.     ' while the string contains spaces...
  22.     Do While position > 0
  23.         position = InStr(position, Str1, "" "")
  24.         ' ... increase word count
  25.         If position > 0 Then
  26.             words = words + 1
  27.             ' and skip additional spaces
  28.             While Mid(Str1, position, 1) = "" ""
  29.                 position = position + 1
  30.             Wend
  31.         End If
  32.     Loop
  33.     Show ""The string contains "" & words & "" words""
  34.  
  35. End Sub   "
  36.