如何查找字符串中的单词数?

问题描述:

Public Sub word()
            Dim l, i, number As Integer
            no = 0
            Console.WriteLine("Type the text")
            s = Console.ReadLine()
            l = Len(s)
            Do While (i < l)
                If s(i) = "" Then
                    no = no + 1

                End If
                i = i + 1
                number = no
            Loop

            Console.WriteLine("The no of words is {0}", number)
        End Sub
    End Class





我尝试过:



i已经尝试了很多次。比如说没有单词是0.



What I have tried:

i have tried a lot of times .it says the no of words is 0.

使用拆分() http://www.dotnetperls.com/split-vbnet [ ^ ]


你应该试试:

You should try:
If s(i) = " " Then



a空格被加入之间



否则,您可以简化代码:


a space was added between quotes

Otherwise, you can simplify your code:

    Public Sub word()
        Dim l, i , number As Integer
        no = 0
        Console.WriteLine("Type the text")
        s = Console.ReadLine()
        l = Len(s)
        Do While (i < l)
            If s(i) = " " Then
                no = no + 1

            End If
            i = i + 1
            number = no
        Loop

        Console.WriteLine("The no of words is {0}", number no)
    End Sub
End Class


请先阅读我对该问题的评论。正如我在那里提到的,它取决于你定义的单词。



有很多类似问题 [ ^ ]。例如:

如何在单词中拆分字符串 [ ^ ]
Please, read my comment to the question first. As i mentioned there, it depends on what you define as a word.

There's a lot of similar questions[^] on this forum. For example:
How to split string in words[^]