Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 查找数组中单词的长度-Visual Basic_Vb.net_Visual Studio 2010 - Fatal编程技术网

Vb.net 查找数组中单词的长度-Visual Basic

Vb.net 查找数组中单词的长度-Visual Basic,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,我有一个richtextbox,我将单词转换成数组,然后我有代码,它将获取长度并输出它。。。唯一的问题是我不知道如何循环数组的所有不同值来检查数组中每个单独部分的长度 (我把这台电视机换了) 你可以使用另一个数组吗 Dim arr(14) as integer Dim input As String Dim words As String() Dim length As Integer input = RichTextBox1.Text words = Split(input, " ")

我有一个richtextbox,我将单词转换成数组,然后我有代码,它将获取长度并输出它。。。唯一的问题是我不知道如何循环数组的所有不同值来检查数组中每个单独部分的长度

(我把这台电视机换了)


你可以使用另一个数组吗

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
是一个变量,它在快捷方式代码
中以文字形式为每个w声明

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

如果不使用快捷方式,则执行此操作的较长方法如下:

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

bendataclear向您展示了如何使用For下一个循环。如果可以使用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

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

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

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

请澄清:

w是一个字符串。编译器在设计时确定类型。看到这条S.O.线了吗

编译器通过查看IEnumerable集合(数组、列表等)中的元素来推断类型。intellisense甚至在设计时知道它是字符串,即使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

我有点困惑。。。你想在这里完成什么-似乎应该有一个更有效的方法…你尝试了什么?我知道你发了一些代码,但我看不到有人试图循环使用这些单词。好吧,这对我来说很有意义,只有两个问题:1。“w”是您没有声明的变量吗?或者是什么?另外,Dim l作为Integer=Math.Min(Len(w)-1,14)arr(l)=arr(l)+1“部分在做什么?对于其他长度,我需要做arr(2)、arr(3)等吗?谢谢。我更倾向于您提供的第二种方法,VB说UBound不是system.array“words”的一部分。在该代码中……w=words(I)是什么做什么?对不起,
Ubound
来自VB6,正确的.net方法应该是
GetUpperBound(0)
,添加到上面的示例中。好的,我把它添加到了代码中(我很确定我做错了什么)…(我编辑了我最初发布的带有更改的代码)您没有在每次迭代后设置
length
变量,您需要添加
length=Len(w)
。我刚接触VB一个半月了,所以我不知道LINQ是什么,也不知道如何将它包含在我的代码中?感谢您的澄清,我能够让它正常工作。我用我的最终代码编辑了原始代码。我只是注意到,当我按enter键时,它会认为它是单词的另一个字母,直到我输入一个空格为止……我怎么能忽略“.”/“,”“()”等……输入=输入。删除(“/”)?好的,我可以删除“.”但我如何为“输入”执行此操作?David,我不在工作,也没有在我所在的位置安装VS。我可以明天查看。现在,查看
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