2

我有一个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

4 回答 4

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 只获取单词长度,第二个返回一个包含每个单词及其长度的数组。

于 2012-10-24T14:26:41.690 回答
0

您可以忽略双空格并输入,因为它们被认为是空的:

    'HOW CAN I EXCLUDE DOUBLE SPACES 
    'AND INCLUDE IF SOMEBODY PRESSES ENTER?
    words = input.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
于 2012-10-24T14:39:01.903 回答
0

大卫,

给你一些澄清:

w 是一个字符串。编译器在设计时确定类型。请参阅此 SO 线程 What does VB.Net For Each Loop 查看以推断类型

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

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

但。您当然可以显式添加类型

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
于 2012-10-25T16:36:55.807 回答
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
于 2012-10-24T13:54:53.823 回答