在数组中查找单词的长度 - Visual Basic

时间:2012-10-24 13:22:06

标签: vb.net visual-studio-2010

我有一个richtextbox,我将单词转换为数组,然后我有代码,它将取长度和输出它...唯一的问题是我不知道如何循环遍历所有不同的值用于检查数组中每个部分长度的数组。

(我在TextChanged上设置了这个)

        Dim len1, len2, len3, len4, len5, len6, len7, len8, len9, len10, len11, len12, len13, len14, len15 As Integer
    Dim input As String
    Dim words As String()
    Dim length As Integer

    input = RichTextBox1.Text


    words = input.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)

    For Each w In words

        length = Len(w)

        Select Case length


            Case 1
                len1 = len1 + 1
            Case 2
                len2 = len2 + 1
            Case 3
                len3 = len3 + 1
            Case 4
                len4 = len4 + 1
            Case 5
                len5 = len5 + 1
            Case 6
                len6 = len6 + 1
            Case 7
                len7 = len7 + 1
            Case 8
                len8 = len8 + 1
            Case 9
                len9 = len9 + 1
            Case 10
                len10 = len10 + 1
            Case 11
                len11 = len11 + 1
            Case 12
                len12 = len12 + 1
            Case 13
                len13 = len13 + 1
            Case 14
                len14 = len14 + 1
            Case 15
                len15 = len15 + 1

        End Select

    Next








    letcount.onelet.Text = Val(len1)
    letcount.twolet.Text = Val(len2)
    letcount.threelet.Text = Val(len3)
    letcount.fourlet.Text = Val(len4)
    letcount.fivelet.Text = Val(len5)
    letcount.sixlet.Text = Val(len6)
    letcount.sevenlet.Text = Val(len7)
    letcount.eightlet.Text = Val(len8)
    letcount.ninelet.Text = Val(len9)
    letcount.tenlet.Text = Val(len10)
    letcount.elevenlet.Text = Val(len11)
    letcount.twelevelet.Text = Val(len12)
    letcount.thirteenlet.Text = Val(len13)
    letcount.fourteenlet.Text = Val(len14)
    letcount.fifteenlet.Text = Val(len15)

4 个答案:

答案 0 :(得分:1)

bendataclear向您展示了如何使用For Next循环。如果可以使用LINQ,则可以使用以下代码:

Sub Main
    Dim words As String() = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }

    Dim lengthsOnly = words.Select(Function(w) w.Length).ToArray()

    Dim wordsAndLengths = words.Select(Function(w) New With { .Word = w, .Length = w.Length }).ToArray()

End Sub

第一个Select只获取单词长度,第二个返回一个包含每个单词及其长度的数组。

答案 1 :(得分:0)

您可以使用其他阵列吗?

Dim arr(14) as integer

Dim input As String
Dim words As String()
Dim length As Integer

input = RichTextBox1.Text

words = Split(input, " ")

For Each w in words

    Dim l as Integer = Math.Min(Len(w) - 1,14)
    arr(l) = arr(l) + 1

Next

修改

回答评论中的问题:

w是一个变量,它在快捷键For Each w in words

中声明

快捷方式声明变量w,然后使用数组words的每个成员填充它,并在Fornext之间运行代码。

如果没有快捷方式,更长的方法就是:

Dim i as Integer

For i = 0 to words.GetUpperBound(0) -1

    Dim w as string

    w = words(i)

    i = i + 1

    'Rest of code

Next i

下一部分Dim l as Integer = Math.Min(Len(w) - 1,14)l设置为单词的长度,最多为14,好像我们有一个超过15个字母的单词,它不适合数组。

另一种方法是:

Dim l as Integer 

l = Len(w) - 1

If l > 14 then
    l = 14
End If

arr(l) = arr(l) + 1

答案 2 :(得分:0)

您可以忽略双倍空格并输入,因为它们被视为空:

    'HOW CAN I EXCLUDE DOUBLE SPACES 
    'AND INCLUDE IF SOMEBODY PRESSES ENTER?
    words = input.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)

答案 3 :(得分:0)

大卫,

为您澄清一下:

w是一个字符串。编译器在设计时确定类型。见S.O.线 What does VB.Net For Each Loop look at to Infer the Type

Dim words() As String = {"0", "1", "2"}
For Each w In words
    System.Windows.Forms.MessageBox.Show(w)
Next

编译器通过查看IEnumerable集合中的元素(数组,列表等)来推断类型。智能感知甚至在设计时知道它是一个字符串,即使w看起来像是被定义为泛型。亲自试试......

BUT。您当然可以明确添加类型

Dim words() As String = {"0", "1", "2"}
For Each w As String In words
    System.Windows.Forms.MessageBox.Show(w)
Next

字符串数组示例并未真正展示速记的便利性。见下文

Dim dictionaryOfDictionaries As New Dictionary(Of String, Dictionary(Of String, String))
For Each d As KeyValuePair(Of String, Dictionary(Of String, String)) In dictionaryOfDictionaries
    ' do something with each dictionary d
Next
' is identical to
For Each d In dictionaryOfDictionaries
    ' do something with each dictionary d
Next