The Split function allows you to separate long strings into separate words. However, if there is more than one space between words, Split returns an error. To prevent this, you can use the Replace function to Replace multiple spaces before using Split. List A provides an example.
List
Private Sub CountWords () Dim strText As String = "It's a wonderful world" Dim iCount As IntegerDo While (strText. indexOf (Space (2)> = 0) strText = strText. replace (Space (2), Space (1) LoopiCount = Split (strText, Space (1 )). lengthMsgBox (iCount. toString () End Sub
In this example, I created the strText string and set it to a long string with multiple characters. Then, I use the Replace function to Replace multiple spaces with one. This is done to prepare the string strText so that you can use the Split function and provide the correct results.
Then, I input strText into the Split function and get the number of words included in the string strText. Note: If you skip or annotate the loop to Remove extra spaces, the result is 7 words. The result is correct after all the unnecessary spaces are removed.
Private Sub CountWords () Dim strText As String = "It's a wonderful world" Dim iCount As IntegerDo While (strText. indexOf (Space (2)> = 0) strText = strText. replace (Space (2), Space (1) LoopiCount = Split (strText, Space (1 )). length MsgBox (iCount. toString () End Sub