Use Replace & Split to count words in VB.Net
Whenever need to know the number of words in a larger VB.NET string consider using the Split function. In this example of how to use Split, as well as Replace if you have more than one space between words.
Working with strings and performing various string operations is important to any Visual Basic developer. Whenever you need to be able to determine the number of words in a larger string, there is a simple VB.NET function that you can use in order to get the necessary result.
The Split function allows you to separate the longer string into words; however, if you have more than one space between words, Split can return an incorrect result. To prevent that from happening, you can use a Replace function before using Split in order to change any occurrence of multiple spaces. Snippet A offers an example.
Note: If you remove or comment the loop that removes extra spaces, the result is seven words. With the loop that removes all extra spaces, the result is four words, which is correct.
|All times are GMT +5.5. The time now is 18:28.|