首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除输出中的重复行?

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

Stack Overflow用户
提问于 2016-08-23 23:26:15
回答 2查看 40关注 0票数 0
代码语言:javascript
复制
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

几乎完成了,但问题是输出重复了一些行,如:

请输入一个句子

苹果从苹果树上掉下来

苹果这个词位于第二位。

苹果这个词位于第六位。

苹果这个词位于第二位。

苹果这个词位于第六位。

帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-24 00:03:24

我不太明白你的循环背后的道理。您要做的是:搜索数组中的单词。如果它存在,打印它的位置。否则退出。重新开始。

我会这样做:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2016-08-24 00:09:10

试试这个,对不起,但我没有想知道你做的那个有什么问题。

代码语言:javascript
复制
 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39112192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档