This is a visual basic 6.0 source code that counts the number of words in a specified string. This function can be useful in Text editors or where you want to tell the user that how much words have been inputted in the text field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | '******************************************************* '* MYCPLUS Sample Code - https://www.mycplus.com * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* Please direct all inquiries to saqib at mycplus.com * '******************************************************* Public Function GetWordCount(ByVal Text As String) As Long 'Assume a hyphen at the end of a line 'is part of a full-word, so combine together Text = Trim(Replace(Text, "-" & vbNewLine, "")) 'Replace new lines with a single space Text = Trim(Replace(Text, vbNewLine, " ")) 'Collapse multiple spaces into one single space Do While Text Like "* *" Text = Replace(Text, " ", " ") Loop 'Split the string and return counted words GetWordCount = 1 + UBound(Split(Text, " ")) End Function |