如何查找字符串中的单词数? [英] How to find the number of words in a string?

查看:84
本文介绍了如何查找字符串中的单词数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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[^]


这篇关于如何查找字符串中的单词数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆