如何在visual basic中将整数附加到数组 [英] How to append an integer to an array in visual basic

查看:30
本文介绍了如何在visual basic中将整数附加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,将没有标点符号的句子作为输入,然后搜索特定单词出现的次数.

I'm making a program that takes a sentence with no punctuation as an input, then searches for how many occurrences there are of a specific word.

 Dim sntnce As String
    Dim srchWord As String
    Dim words As String()
    Dim word As String
    Dim count As Integer
    Dim index As New System.Text.StringBuilder()

    Console.WriteLine("Enter your sentence: ")
    sntnce = Console.ReadLine()
    sntnce = LCase(sntnce)

    Console.WriteLine("Enter the word you want to find the position of: ")
    srchWord = Console.ReadLine()
    srchWord = LCase(srchWord)

    words = sntnce.Split(New Char() {" "c})
    Console.WriteLine(" ")

    For Each word In words
        If word = srchWord Then
            index.Append(count)
            count += 1
        Else
            count += 1
        End If
    Next

    Console.WriteLine("Your word appears in the position(s)...")
    Console.WriteLine(index)
    Console.ReadLine()

for 循环获取在句子中找到的单词的索引并将其附加到字符串中,但我想将其附加到数组中,以便可以单独输出索引的值,但是我不能找到任何有帮助的解决方案.我怎样才能做到这一点?谢谢

The for loop takes the index of a word if found in a sentence and appends it to a string, but I would like to append it to an array so that the values for indexes can be outputted separately, however I can't find any solutions that help. How can I do this? Thanks

推荐答案

数组只有固定大小.处理要添加、删除或插入的集合时,请使用 List(Of T).

Arrays are of fixed size only. When dealing with collections that you want to add, remove or insert use List(Of T).

我认为您想要的是以下内容:

I think what you want is the following:

Sub Main()
    Dim sntnce As String
    Dim srchWord As String
    Dim words As String()
    Dim word As String
    Dim count As Integer

    Console.WriteLine("Enter your sentence: ")
    sntnce = Console.ReadLine()
    sntnce = LCase(sntnce)

    Console.WriteLine("Enter the word you want to find the position of: ")
    srchWord = Console.ReadLine()
    srchWord = LCase(srchWord)

    words = sntnce.Split(New Char() {" "c})
    Console.WriteLine(" ")
    ' Create an expandable list of integers 'index_list'
    Dim index_list As New List(Of Integer)
    For index As Integer = 1 To words.Length
        ' It's recommened to use `Equals()` for string equality instead of `=`
        ' Arrays in VB.NET are 0-based so the i-th elements is located in `list(i-1)`
        If words(index - 1).Equals(srchWord) Then
            ' Add index to list if there is a match
            index_list.Add(index)
        End If
    Next
    ' Create a fixed array of strings to hold the character representation of the integer index array. 
    ' Since I know the size I can use an array instead of a list.
    Dim index_list_string As String() = New String(index_list.Count) {}
    For index As Integer = 1 To index_list.Count
        ' One to one mapping of integer to string
        index_list_string(index - 1) = index_list(index - 1).ToString()
    Next
    Console.WriteLine("Your word appears in the position(s)...")
    ' Take the array of strings and combine it into one string with commas in between using the `.Join()` function
    ' For example `{"A","B","C"} => "A, B, C"
    Console.WriteLine(String.Join(", ", index_list_string))
    Console.ReadLine()
End Sub

这篇关于如何在visual basic中将整数附加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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