VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Count words in a string with 2 lines of code

by IceZer0 Software (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

Have you been using the instr function, or 3rd party functions to find the word count of a block of text. Now you find it in ONLY 2 LINES OF CODE - NO API CALLS - NO MODULES / CLASSES / CONTROLS - PURE VB CODE

Rate Count words in a string with 2 lines of code

Public Function WordCount(Text as String) as Long
'Declare an Array
Dim splitText() as String
splitText = Split(Text, " ") 
'Split it using a space charachter for where to split it.
WordCount = Ubound(splitText) + 1 
'Set the function return to the end of the array. Ex: See Spot Run! would make splitText(0) = See, splitText(1) = spot, and splitText(2) = Run! - 2 is the highest so 1 is added to it to make the word count
'Good luck on your vb programming - IceZero
End Function

Download this snippet    Add to My Saved Code

Count words in a string with 2 lines of code Comments

No comments have been posted about Count words in a string with 2 lines of code. Why not be the first to post a comment about Count words in a string with 2 lines of code.

Post your comment

Subject:
Message:
0/1000 characters