如何删除输出中的重复行?

时间:2016-08-23 23:26:15

标签: vb.net indexof find-occurrences

If ArrySentence.Contains(InputString) Then
    Dim index As Integer = Array.IndexOf(ArrySentence, InputString)

    Do Until a >= ArrySentence.GetUpperBound(0) + 1
        Dim position As Integer = index + 1
        index = Array.IndexOf(ArrySentence, InputString, position)

        Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, position)
        a = a + 1
    Loop

这几乎已经完成,但问题是输出重复了一些行,如:
请输入句子
苹果从苹果树上掉下来 苹果这个词位于第2位。苹果这个词位于第6位。苹果这个词位于第2位。苹果这个词位于第6位

帮助!

2 个答案:

答案 0 :(得分:0)

我真的不明白你的循环背后的原因。您要做的是以下内容:在数组中搜索单词。如果存在,请打印其位置。否则退出。重来。

我将如何做到这一点:

Dim index As Integer = Array.IndexOf(ArrySentence, InputString)
While index >= 0 'while the word has been found
    'Output the occurrence
    Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, index + 1)
    'Search for the next occurrence
    index = Array.IndexOf(ArrySentence, InputString, index + 1)
End While

答案 1 :(得分:0)

试试这个,对不起,但我并没有试图找出你所做的那个错误。

 Dim sentence as String = "The apple fall from the apple tree"
 Dim words As String() = sentence.Split(New Char() {" "c})

 For i = 0 To words.Length - 1
     If InputString = words(i) Then
          Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, i + 1)
     End If
 Next